0

这是我所拥有的:

我动态创建了这个标记:

 <p id="amount"><span></span></p>

现在我需要填写跨度:

if (amount.firstChild.nodeType == 1) {
  amount.firstChild.nodeValue = text;

  alert(description.firstChild.nodeType); //this shows SPAN

}

amount 是一个使用 getElementbyId 创建的变量,而 text 是一个可以工作的变量,只显示一个数字。

现在,为什么不接受文本作为值?它呈现和空跨度......并且文本工作得很好......

非常感谢伙计们!

4

3 回答 3

2

尝试使用:

amount.firstChild.appendChild(document.createTextNode(text));

JS Fiddle 概念验证

于 2012-05-07T18:52:07.330 回答
1
amount.firstChild.innerHTML = text;
于 2012-05-07T18:49:27.307 回答
0

您可以测试跨度中的嵌套文本节点,如果没有,则创建它。

var span = amount.firstChild
if (!span.firstChild)
    span.appendChild(document.createTextNode(text));
else
    span.firstChild.nodeValue = text;
于 2012-05-07T18:53:21.203 回答