Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
152 views
in Technique[技术] by (71.8m points)

javascript - jQuery计数子元素(jQuery count child elements)

 <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> .(我想计算<div id="selected"></div><li>元素的总数。)

How is that possible using jQuery's .children([selector]) ?(怎么可能使用jQuery的.children([selector]) ?)   ask by gautamlakum translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can use .length with just a descendant selector , like this:(你可以使用.length只有一个后代选择器 ,如下所示:)

var count = $("#selected li").length; If you have to use .children() , then it's like this:(如果你必须使用.children() ,那么它是这样的:) var count = $("#selected ul").children().length; You can test both versions here .(你可以在这里测试两个版本 。)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...