[英]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.