简体   繁体   English

在每个列表项中搜索项

[英]Searching for item in each list item

I am working on a piece of code to check if there is in the list item a div called star rating. 我正在编写一段代码,以检查列表项中是否有一个称为星级的div。 If it doesnt excist, add to the price tag an extra margin top (so i can level out these items). 如果它不存在,请在价格标签上添加一个额外的保证金顶部(以便我可以平整这些项目)。 My JQuery is as followed 我的JQuery如下

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

This code works fine to a certain point. 该代码在一定程度上可以正常工作。 What it technically does is it doesnt check the specific list item but the whole document. 它在技术上所做的是它不检查特定列表项而是整个文档。 Is there a way I can just check the specific list item and add to the span.price in that item the css? 有没有办法我可以只检查特定的列表项,然后将css添加到该项的span.price中? cause now it finds 4 times the star-rating (4 list items), while it is only in one list item. 因为现在它找到的是星级评分的4倍(4个列表项),而它仅是一个列表项。

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

Add context parameter to jQuery method 将上下文参数添加到jQuery方法

Search under each "li.product" : 在每个"li.product"下搜索:

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

没有“ if”和“ each”,这就是Jquery的目的...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM