简体   繁体   English

为什么在循环中改变颜色后颜色不会改变

[英]Why colour does not change after change colour in loop

I have the following html code: 我有以下HTML代码:

<div class="clm class2"></div>
<div class="clm class3"></div>
<div class="clm class4"></div>
<div class="clm class3"></div>
<div class="clm class4"></div>

And I have the following css code: 我有以下css代码:

.clm{
    width:50px;
    height:50px;
    margin-bottom: 10px;
    border-color: black;
}

.class1{
    background: red;
}

.class2{
    background: green;
}

.class3{
    background: blue;
}

.class4{
    background: black;
}

When I clicked div block with class class2 , I need change all div's with class3 to class4 当我用class class2点击div块时,我需要将class3class4

For example: 例如:

before: <div class="clm class3"></div> 之前: <div class="clm class3"></div>

after click: <div class="clm class4"></div> 点击后: <div class="clm class4"></div>

I have the following js code (jQuery): 我有以下js代码(jQuery):

$(".class2").click(function(){
        $(".class3").each(function() {
            $(this).attr('class','clm class4');
        });
    });

This code work fine. 这段代码很好用。 The colour is changed. 颜色改变了。 I have the following other js script (jQuery): 我有以下其他js脚本(jQuery):

$(".class4").click(function(){
        $(this).attr('class','clm class1');
    });

If I clicked to div with class4 before click div with class2 the code work ok, but when I clicked to div with class2 and I try to change colour click in div with class4 it's does not work. 如果我在使用class2单击div之前点击div与class4代码工作正常,但是当我用class2单击div时,我尝试更改颜色在div中使用class4单击它不起作用。

PS the html code changed correctly and I see right colour. PS html代码改变正确,我看到正确的颜色。

You need to use event delegation for all class names that are added/removed dynamically. 您需要对动态添加/删除的所有类名使用事件委派。

Event delegation allows us to attach a single event listener, to a parent element, that will fire for all descendants matching a selector, whether those descendants exist now or are added in the future. 事件委托允许我们将单个事件侦听器附加到父元素,该元素将为匹配选择器的所有后代触发,无论这些后代现在是存在还是将来添加。

$("body").on('click','.class4',function(){
    $(this).attr('class','clm class1');
});

The problem is that when you bind the $('.class4') click handler, the elements to which you're dynamically adding the .class4 classname to do not exist. 问题是,当您绑定$('.class4')单击处理程序时,您动态添加.class4类名的元素不存在。 You need to use event delegation as follows: 您需要使用事件委派 ,如下所示:

$('body').on('click', '.class4', function() {
    $(this).attr('class', 'clm class1');
});

jsFiddle Demo jsFiddle演示

As a post-script to the above, it's also worth noting that using addClass() and removeClass() is apparently much faster than setting the class attribute using attr() . 作为上述的后脚本,还值得注意的是,使用addClass()removeClass()显然比使用attr()设置class属性快得多。

Use event delegation for dynamically created class in the DOM. 在DOM中为动态创建的类使用事件委托 Otherwise DOM didn't know whether the class is found or not. 否则DOM不知道是否找到了这个类。 In these use event delegation it traverse once again from the root of the body and find the class is found or not and 在这些使用事件委托中,它再次从身体的根部遍历并找到该类是否被发现

Note: Use .addClass() for added new classes instead of .attr() 注意:使用.addClass()添加新类而不是.attr()

$("body").on('click','.class4',function(){
    $(this).addClass('clm class1');
});

我觉得这可以用$('。class3')。removeClass(“class3”)。addClass('class4');

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

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