简体   繁体   English

jQuery计算父母中最大的孩子数

[英]jQuery counting highest number of children in parents

I'd appreciate help on the following problem: Let's say I have a number of ul elements on a page, each with a number of li children, eg 感谢您解决以下问题:假设我在页面上有多个ul元素,每个元素都有许多li子元素,例如

<ul>
    <li></li>
    <li></li>
</ul>
<ul>
    <li></li>
    <li></li>
    <li></li>
</ul>

What would be the best way to find the highest number of children across all ul parents (In the example above, it should return 3 )? 在所有ul父母中找到最多子女数量的最佳方法是什么(在上面的示例中,它应该返回3 )?

Many thanks, Ch 非常感谢,Ch

You just need this: 您只需要这个:

var high = Math.max.apply(null, $('ul').map(function () {
    return $(this).children().length;
}).get());
alert(high);

DEMO DEMO

You can easily check how many li elements ul containing by checking length 您可以通过检查length轻松检查ul包含多少li元素

Try FIDDLE 尝试FIDDLE

var highest='0';
$('ul').each(function(){
  var elem=$(this).find('li').length; 
  if(elem >= highest)
     highest=elem;
});

alert("Highest no. of li elements is "+highest);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM