JavaScript中特定标签包含的元素数量?
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