2015-12-29 31 views
0

我正在處理一段代碼以檢查列表項中是否存在稱爲星級的div。如果它不刺激,添加價格標籤額外的保證金頂部(所以我可以平整這些項目)。我的jQuery是如下在每個列表項中搜索項目

$(document).ready(function() { 
    $("li.product").each(function(){ 
     if($(".star-rating").length === 0) {    
      $("span.price").css({"margin-top" : "1em"}); 
     } 
    }); 
}); 

此代碼工作正常到某一點。它在技術上做的是不檢查特定列表項目,而是檢查整個文檔。有沒有辦法,我可以檢查具體的清單項目,並將該項目添加到span.price該css?因爲現在它發現4次星級(4個列表項目),而它只在一個列表項目中。

回答

2

搜索下的每個"li.product"

$("li.product").each(function(){ 
    if($(this).find(".star-rating").length === 0) {    
     $(this).find("span.price").css({"margin-top" : "1em"}); 
    } 
}); 
2
if($(".star-rating",this).length === 0) {    
      $("span.price",this).css({"margin-top" : "1em"}); 
     } 

添加上下文參數到jQuery的方法

0
$('li.product').not(':has(.star-rating)').find("span.price").css({"margin-top" : "1em"}); 

沒有 「如果」 和 「各」 這是jQuery是什麼?