简体   繁体   English

jQuery:根据条件将类添加到元素

[英]JQuery : Add class to a element based on a condition

In the below code snippet if a div with strikeWindow class has " display:none " style then the corresponding div with class blockElement should be added a class " strikeWindowAddtocart " 在下面的代码段中,如果与strikeWindow类div有“ 显示:无 ”的风格然后用类blockElement相应的div应加一类“strikeWindowAddtocart”

<div class="boxContent" >
    <div class="strikeWindow" style="display:none">
    </div>
    <div class="blockElement">
    </div>    
</div>
<div class="boxContent" >
    <div class="strikeWindow" style="display:block">
    </div>
    <div class="blockElement">
    </div>     
</div>

I tried the following jquery snippet, but it dint work. 我尝试了以下jquery代码段,但它的确可行。 I would love to have a working JSFiddle example. 我希望有一个工作的JSFiddle示例。 Thanks. 谢谢。

$('.strikeWindow').each(function(i, obj) {
    if((obj.style.display).search('block') ==0) {
        $(this).parents('.boxContent').find(".blockElement").addClass("strikeWindowAddtocart");
    }
});

You may try something like this: 您可以尝试如下操作:

 $('.boxContent>div.strikeWindow:hidden') .next('.blockElement') .addClass('strikeWindowAddtocart'); 
 .strikeWindowAddtocart{ background:red; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="boxContent" > <div class="strikeWindow" style="display:none">I'm Hidden</div> <div class="blockElement">BlockElement after hidden strikeWindow</div> </div> <div class="boxContent" > <div class="strikeWindow" style="display:block">Visible strikeWindow</div> <div class="blockElement">BlockElement after visible strikeWindow</div> </div> 

You can do it like this: 您可以这样做:

$('.strikeWindow').each(function (i, obj) {
    alert($(obj).css("display") == "none") {
        $(this).parents('.boxContent').find(".blockElement").not(this).addClass("strikeWindowAddtocart");
    }
});

I tested this code and it works: 我测试了这段代码,它的工作原理是:

$('.strikeWindow').each(function() {
    if ($(this).css("display") == "none")
        $(this).next("div").addClass("strikeWindowAddToCart");
});

It can be done as : 可以这样做:

$('.strikeWindow').each(function(i, obj) {
    if($(this).css("display") == "none") {      // you made a mistake here
       $(this).parents('.boxContent').find(".blockElement").addClass("strikeWindowAddtocart");
    }
});

For checking whether an element has a particular style or not .css method of jquery can be used. 为了检查元素是否具有特定样式,可以使用jquery的.css方法。

You can also do this by using the following code: 您也可以使用以下代码来执行此操作:

var x = $(".strikeWindow").length
for(var i=1; i<=x;i++)
{
    if ($(".strikeWindow:nth-child('+i+')").css("display")=="none")
        $($(".strikeWindow:nth-child('+i+')")).next("blockElement").addClass("strikeWindowAddtocart");
}

A working JSFiddle example: http://jsfiddle.net/bbc5rp76/1/embedded/result,html,js,css/ 一个有效的JSFiddle示例: http : //jsfiddle.net/bbc5rp76/1/embedded/result,html,js,css/

$(".strikeWindow").each(function () {
    var $this = $(this);
    if ($this.css("display") === "none") $this.siblings(".blockElement").addClass("strikeWindowAddtocart");
});

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

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