質問

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:

  1. count children of each ul, storing the lengths in an array
  2. pick the longest length and store it in a new variable

Thanks for any help!

役に立ちましたか?

解決

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

他のヒント

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 ;
});
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top