查找孩子最多的元素

use*_*342 2 jquery children for-loop

我有一个带有各种嵌套无序列表的导航栏。我需要做的是计算每个 UL 的子级并设置一个包含最高计数数字的变量。

虽然我认为我可以使用 for/while 循环来做到这一点,但我不太确定如何去做。

该脚本将:

  1. 计算每个 ul 的子级,将长度存储在数组中
  2. 选择最长的长度并将其存储在新变量中

谢谢你的帮助!

Kri*_*hna 5

var arr = []; //populate the length of children into this array.
$('ul').map(function (i) {
    arr[i] = $(this).children().length;
});
var maxValue = Math.max.apply(Math, arr); //get the max value from the array
Run Code Online (Sandbox Code Playgroud)