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
Find element with most children [closed]
質問
I have a navigation bar with various nested unordered lists. What I need to do is count the children of each UL and set a variable containing the highest counted number.
While I think I could do this using a for/while loop I'm not exactly sure how to go about it.
The script will:
- count children of each ul, storing the lengths in an array
- pick the longest length and store it in a new variable
Thanks for any help!
解決
他のヒント
IF your navigation is an 'ul' and you have to find the one have maximum children 'li' then :
var maximum = 0;
$('ul').each(function(){
var height = $(this).height();
if(height > maximum )
maximum = maximum ;
});
所属していません StackOverflow