<!DOCTYPE html> <html> <body> <h1>Element 对象</h1> <h2>childNodes 属性</h2> <div id="myDIV" style="background:coral; padding:16px;"> <p>第一个 p 元素 (index 1)</p> <p>第二个 p 元素 (index 3)</p> </div> <p>"myDIV" 中的子节点数为:</p> <p id="demo"></p> <p>元素之间的空白是文本节点。在此例中,"myDIV" 中的索引 0、2 和 4 是文本节点。</p> <script> const element = document.getElementById("myDIV"); let numb = element.childNodes.length; document.getElementById("demo").innerHTML = numb; </script> </body> </html>