如何计算总LI宽度并设置父div的宽度。
我的示例代码:
<ul>
<li>dsfdsfdsfds</li>
<li>dsfdsfdsfds</li>
<li>dsfdsfdsfds</li>
<li>dsfdsfdsfds</li>
<li>dsfdsfdsfds</li>
<li>dsfdsfdsfds</li>
</ul>
Css风格:
ul li{width:400px;float:left;}
答案 0 :(得分:2)
使用此代码
<script type="text/javascript" src="public/js/jquery-1.4.2.min.js" ></script>
<script type="text/javascript">
$(document).ready(function(){
$('.nested_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);
}
});
});
</script>