[英]How to sum width of list items has top equal to 0px
HTML:
<ul class="listul">
<li style="width:10px; top: 0px"></li>
<li style="width:10px; top: 0px"></li>
<li style="width:10px; top: 5643px"></li>
<li style="width:10px; top: 56px"></li>
</ul>
JS:
var top = $('.listul li').css('top');
var wT = 0;
if (top = '0px'){
$(this).each(function(){
wT += $(this).outerWidth();
});
alert(wT);
}
我想用if(top ='0px')将li的宽度求和,但这不起作用,请帮帮我
要循环每个列表项并检查top
值,您应该尝试:
$(function(){
var wT = 0;
var items = $('.listul li');
items.each(function(){
if($(this).css('top') == "0px"){
wT += $(this).outerWidth();
}
});
alert(wT);
});
见 JsFiddle
在li的样式标签中指定width:10px。 不是width10px
你可以这样做:
var wT = 0;
$('.listul li').each(function(index, element) {
var $element = $(element);
if ($element.css('top')=='0px') {
wT+=$element.outerWidth();
}
});
alert(wT);
这是演示 : JSFiddle
您的html代码必须是这样的:
<ul clas="listul">
<li style="width: 10px; top: 0px;"></li>
<li style="width: 10px; top: 0px;"></li>
<li style="width: 10px; top: 5643px;"></li>
<li style="width: 10px; top: 56px;"></li>
</ul>
希望这对你有帮助。
您应该在if语句中使用比较运算符(==)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.