运行 ❯
获取您
自己的
网站
×
更改方向
保存代码
更改主题,深色/浅色
前往 Spaces
<!DOCTYPE html> <html> <body> <h1>The Element Object</h1> <h2>The childNodes Property</h2> <div id="myDIV" style="background:coral; padding:16px;"> <p>First p element (index 1)</p> <p>Second p element (index 3)</p> </div> <p>The number of child nodes in "myDIV" are:</p> <p id="demo"></p> <p>Whitespace between elements are text nodes. In this example, index 0, 2 and 4 in "myDIV" are text nodes.</p> <script> const element = document.getElementById("myDIV"); let numb = element.childNodes.length; document.getElementById("demo").innerHTML = numb; </script> </body> </html>