源代码:
下载代码
AI 编程工具
点击运行
<!DOCTYPE html> <html> <head> <style> div { border: 1px solid black; margin: 5px; } </style> </head> <body> <p>点击按钮查看 div 有几个子元素。</p> <button onclick="myFunction()">点我</button> <div id="myDIV"> <p>第一个 p 元素 (索引为 0)</p> <p>第二个 p 元素 (索引为 1)</p> </div> <p id="demo"></p> <script> function myFunction() { var c = document.getElementById("myDIV").children.length; document.getElementById("demo").innerHTML = c; } </script> </body> </html>
运行结果: