简体   繁体   English

删除除一个以外的所有类

[英]Remove all classes except one

Well, I know that with some jQuery actions, we can add a lot of classes to a particular div: 好吧,我知道通过一些jQuery动作,我们可以为特定的div添加很多类:

<div class="cleanstate"></div>

Let's say that with some clicks and other things, the div gets a lot of classes 让我们说,通过点击和其他一些事情,div可以获得很多课程

<div class="cleanstate bgred paddingleft allcaptions ..."></div>

So, how I can remove all the classes except one? 那么,我如何删除除一个类之外的所有类? The only idea I have come up is with this: 我提出的唯一想法就是:

$('#container div.cleanstate').removeClass().addClass('cleanstate');

While removeClass() kills all the classes, the div get screwed up, but adding just after that addClass('cleanstate') it goes back to normal. 虽然removeClass()杀死所有类,但div会搞砸,但是在addClass('cleanstate')之后添加它会恢复正常。 The other solution is to put an ID attribute with the base CSS properties so they don't get deleted, what also improves performance, but i just want to know another solution to get rid of all except ".cleanstate" 另一个解决方案是使用基本CSS属性放置一个ID属性,这样它们就不会被删除,这也会提高性能,但我只是想知道另一个解决方案除去“.cleanstate”之外的所有解决方案。

I'm asking this because, in the real script, the div suffers various changes of classes. 我问这个是因为,在真实的脚本中,div会遭受各种类的更改。

Instead of doing it in 2 steps, you could just reset the entire value at once with attr by overwriting all of the class values with the class you want: 您可以通过使用所需的类覆盖所有类值来使用attr立即重置整个值,而不是分两步完成:

jQuery('#container div.cleanstate').attr('class', 'cleanstate');

Sample: http://jsfiddle.net/jtmKK/1/ 示例: http//jsfiddle.net/jtmKK/1/

使用attr直接将class属性设置为您想要的特定值:

$('#container div.cleanstate').attr('class','cleanstate');

使用普通的旧JavaScript,而不是JQuery:

document.getElementById("container").className = "cleanstate";

Sometimes you need to keep some of the classes due to CSS animation, because as soon as you remove all classes, animation may not work. 有时你需要保留一些类,因为CSS动画,因为一旦删除所有类,动画可能无法正常工作。 Instead, you can keep some classes and remove the rest like this: 相反,你可以保留一些类,并删除其余的如下:

$('#container div.cleanstate').removeClass('removethis removethat').addClass('cleanstate');

关于抢夺答案,为了完整起见,你也可以使用querySelector和vanilla

document.querySelector('#container div.cleanstate').className = "cleanstate";

What if if you want to keep one or more than one classes and want classes except these. 如果你想保留一个或多个类并且想要除了这些类之外的类,该怎么办? These solution would not work where you don't want to remove all classes add that perticular class again. 如果您不想删除所有类,那么这些解决方案将无法再次添加该类。 Using attr and removeClass() resets all classes in first instance and then attach that perticular class again. 使用attr和removeClass()重置第一个实例中的所有类,然后再次附加该类。 If you using some animation on classes which are being reset again, it will fail. 如果在再次重置的类上使用某些动画,则会失败。

If you want to simply remove all classes except some class then this is for you. 如果你想简单地删除除了某些类之外的所有类,那么这就适合你。 My solution is for: removeAllExceptThese 我的解决方案是:removeAllExceptThese

Array.prototype.diff = function(a) {
            return this.filter(function(i) {return a.indexOf(i) < 0;});
        };
$.fn.removeClassesExceptThese = function(classList) { 
/* pass mutliple class name in array like ["first", "second"] */
            var $elem = $(this);

            if($elem.length > 0) {
                var existingClassList = $elem.attr("class").split(' ');
                var classListToRemove = existingClassList.diff(classList);
                $elem
                    .removeClass(classListToRemove.join(" "))
                    .addClass(classList.join(" "));
            }
            return $elem;
        };

This will not reset all classes, it will remove only necessary. 这不会重置所有类,它只会删除必要的。
I needed it in my project where I needed to remove only not matching classes. 我在我的项目中需要它,我只需要删除不匹配的类。

You can use it $(".third").removeClassesExceptThese(["first", "second"]); 你可以使用$(".third").removeClassesExceptThese(["first", "second"]);

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

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