Javascript提供了childElementCount属性,以返回标签中包含的元素数。返回的值仅包含数字,但不包含诸如元素名称等的值。
document.getElementById("myDIV").childElementCount;它仅采用标签的ID名称,并返回标签中的元素数。
在以下示例中,div标签中有两个元素。使用属性“ childElementCount ”,找到了元素数量,结果显示在输出中。
<html>
<body>
<div id="DIV">
   <p>First element</p>
   <p>Second element</p>
</div>
<p id = "cou"></p>
<script>
   var cou = document.getElementById("DIV").childElementCount;
   document.getElementById("cou").innerHTML = "元素数:" +" "+ cou;
</script>
</body>
</html>First element Second element 元素数: 2