为什么函数 NodeValue__Two()
显示 null
?对我来说,它应该显示与函数 NodeValue__One()
相同的内容。
我已经在 IE6 上测试过了。
<html>
<body>
<script language="JavaScript">
function NodeValue__One()
{
alert(myNodeOne.childNodes(0).nodeValue);//This is OK
}
function NodeValue__Two()
{
alert(document.all[6].nodeValue);//This is NOT OK
}
</script>
<p>This PARAGRAPH has two nodes,
<b id="myNodeOne">Node One Text</b>, and
<b id="myNodeTwo">Node Two Text</b>.
<input id="txt1" type="text" value="Damn!!!" />
</p>
<button onclick="NodeValue__One();">Node Value 1</button></br>
<button onclick="NodeValue__Two();">Node Value 2</button>
</body>
</html>
最佳答案
All
数组是一个元素数组。元素在 nodeValue 中没有值。
另一方面,childNodes
将同时包含 Elements 和 TextNodes。
要获得正确的 All
索引真的很难,因为 All 中列出的实际元素的数量可能与您在 HTML 中看到的不同。例如,尽管 HTML 文本中没有 HEAD 或 TITLE 元素,但它们将出现在 DOM 中。
关于JavaScript - DOM 节点值问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1728084/