简体   繁体   中英

Remove all classes except one

Well, I know that with some jQuery actions, we can add a lot of classes to a particular div:

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

Let's say that with some clicks and other things, the div gets a lot of classes

<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. 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"

I'm asking this because, in the real script, the div suffers various changes of classes.

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:

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

Sample: 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. 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.

If you want to simply remove all classes except some class then this is for you. My solution is for: 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;
        };


I needed it in my project where I needed to remove only not matching classes.

You can use it $(".third").removeClassesExceptThese(["first", "second"]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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