jQuery/JS-计算元素中的元素 [英] jQuery/JS - Count elements within element
本文介绍了jQuery/JS-计算元素中的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在jQuery或JS中,我需要计算父级DIV中称为cont
的DIV元素的数量吗?我在StackOverflow上看到了类似的问题,并尝试了以下方法.
In jQuery or JS I need to count the amount of DIV elements inside my parent DIV called cont
? I've seen similar questions here on StackOverflow and have tried the following.
<div class="b-load" id="cont">
<div>
<img src="page2.jpg" alt=""/>
</div>
<div>
<img src="page3.jpg" alt="" />
</div>
<div>
<img src="page4.jpg" alt="" />
</div>
<div>
<img src="page5.jpg" alt="" />
</div>
</div>
function countPages() {
var maindiv = document.getElementById('cont');
var count = maindiv.getElementsByTagName('div').length;
alert(count);
}
子DIV是动态生成的,因此在页面加载完成后,我需要对它们进行计数.我的问题是我编写的函数计算了13个DIV,在此示例中,应该只有4个!非常感谢您的帮助.
The child DIV's are dynamically produced, so I need to count them after the page has finished loading. The problem I have is the function I wrote counts 13 DIV's and in this example, there should only 4!! Any help gratefully received..
推荐答案
console.log($("#cont div").length);
这篇关于jQuery/JS-计算元素中的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文