add width li's to parent ul
i would like to calculate the width of the li's for each submenu en then put in the ul element.
example
<ul>
<li>level 1</li>
<li>
<ul style="widht:??;"> // total width li's added here 70px
<li>level 2</li> // width of this li 20px
<li>level 2</li>/ width of this li 50px
</ul>
</li>
<li>level 1</li>
<li>level 1
<ul style="widht:??;"> // total width li's added here 70px
<li>level 2</li> // width of this li 20px
<li>level 2</li>/ width of this li 50px
</ul>
</li>
</ul>
i have this so fare
if ('#menu li:has(ul) ul'){
$('li').each(function(){
widthParentUl += $(this).outerWidth(true)
});
$(this)parent.('ul').attr('style','width:' + widthParentUl +'px; display:block; backgro开发者_JS百科und:red;');
}
thnx for the help. I have this now, it's add's the width to its parrent ul.
$('#menu ul li > ul').each(function(){
var liItems = $(this);
var Sum = 0;
if(liItems.children('li').length >= 1){
$(this).children('li').each(function(i, e){
Sum += $(e).outerWidth(true);
});
$(this).width(Sum+1);
}
});
Maybe this helps:
width off all submenu LI's jquery
Base on your comment, give this a shot:
var Sum = 0;
$('ul ul').each(function(){
$(this).children('li').each(function(i, e){
Sum += $(e).outerWidth();
});
$(this).width(Sum);
});
精彩评论