<分区>
问题:如何使用 Javascript 计算 HTML DOM 中的子节点?
这是我的代码;
<div id="myDiv">
<p>First p Element.</p>
<p>Second p Element.</p>
</div>
alert(document.getElementById("myDiv").childNodes.length);
//5
但是我认为有4个子节点...
我的解释:
<p></p> (1st) //Element node
First p Element. (2nd) // text node
<p></p> (3rd) //Element node
Second p Element. (4th) // text node
我想知道第 5 个子节点来自哪里?
关于空格被认为是文本节点,元素什么时候被认为没有子节点?