jQuery 计数子元素 [英] jQuery count child elements
本文介绍了jQuery 计数子元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
<ul><li>29</li><li>16</li><li>5</li><li>8</li><li>10</li><li>7</li>
我想统计
中
元素的总数.使用 jQuery 的 .children([selector])
怎么可能?解决方案
var count = $("#selected li").length;
如果一定要使用.children()
,那么就是这样:>
var count = $("#selected ul").children().length;
<div id="selected">
<ul>
<li>29</li>
<li>16</li>
<li>5</li>
<li>8</li>
<li>10</li>
<li>7</li>
</ul>
</div>
I want to count the total number of <li>
elements in <div id="selected"></div>
. How is that possible using jQuery's .children([selector])
?
解决方案
You can use .length
with just a descendant selector, like this:
var count = $("#selected li").length;
If you have to use .children()
, then it's like this:
var count = $("#selected ul").children().length;
You can test both versions here.
这篇关于jQuery 计数子元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文