简体   繁体   English

jQuery addClass对元素没有影响

[英]JQuery addClass without it having effect on element

I have this class fade that I need my elements to have for external plugins and I have this code where I want an element to fadeIn and Out using JQuery and for that I have to remove de class fade and when I add it again after my fadeIn it fades again. 我有一个此类的fade ,我需要我的元素用于外部插件,并且我有这段代码,我想要一个元素使用JQuery fadefade ,为此,我必须删除de class fade并在我的fadeIn之后再次添加它时它再次消失。

How can I add a class and prevent it from taking action? 如何添加课程并阻止其采取行动?

$.ajax({
    url: BASE_URL+page,
    type: 'get',
    dataType: "html",
    data: {},
    success: function (data, status) {
        if(action == true){
            target.removeClass('fade').fadeOut(500, function(){
                target.html(data).fadeIn(500).addClass('fade'); <-----------
            });
        }
    }
});

It's from bootstrap: 来自引导程序:

.fade {
  opacity: 0;
  -webkit-transition: opacity 0.15s linear;
          transition: opacity 0.15s linear;
}

I'm not sure I understood your requirements, but if you want to have the class fade applied to the element, but you don't want it to have the stylesheet's properties applied to it, you can make use of another class to indicate that the element should not have the effect, and use the pseudo selector not on the stylesheet. 我不确定我是否理解您的要求,但是如果您希望将类fade应用于元素,但是又不想将样式表的属性应用于该元素,则可以使用另一个类来表明元素不应具有效果,并且not在样式表上使用伪选择器。

Like this: 像这样:

// Just add another class with the fade one. "no-effect" for example.
target.html(data).fadeIn(500).addClass('fade no-effect'); 

And use the pseudo not on the css: 并在CSS上not使用伪指令:

.fade:not(.no-effect) {
    opacity: 0;
    -webkit-transition: opacity 0.15s linear;
    transition: opacity 0.15s linear;
}

This way the transition will only be applied to elements that have the .fade class, but don't have the .no-effect class. 这样,过渡将仅应用于具有.fade类但不具有.no-effect类的元素。

Hope it helps someway. 希望它能有所帮助。

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

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