删除除一类之外的所有类

发布于 2024-10-25 08:30:35 字数 619 浏览 3 评论 0原文

好吧,我知道通过一些 jQuery 操作,我们可以向特定的 div 添加很多类:

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

假设通过一些点击和其他操作,div 获得了很多类

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

那么,我如何删除除一个之外的所有类?我想到的唯一想法是:

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

虽然 removeClass() 杀死所有类,但 div 搞砸了,但在 addClass('cleanstate') 之后添加> 它恢复正常。另一个解决方案是将 ID 属性与基本 CSS 属性一起放置,这样它们就不会被删除,这也提高了性能,但我只是想知道另一个解决方案来摆脱除“.cleanstate”之外的所有属性,

我问这个因为,在真实的脚本中,div 会遭受各种类的变化。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

执笏见 2024-11-01 08:30:35

您可以使用 attr 通过使用所需的类覆盖所有类值来立即重置整个值,而不是分两步完成:

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

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

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/

临风闻羌笛 2024-11-01 08:30:35

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

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

Use attr to directly set the class attribute to the specific value you want:

$('#container div.cleanstate').attr('class','cleanstate');
强者自强 2024-11-01 08:30:35

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

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

With plain old JavaScript, not JQuery:

document.getElementById("container").className = "cleanstate";
自此以后,行同陌路 2024-11-01 08:30:35

有时由于 CSS 动画的原因,您需要保留一些类,因为一旦删除所有类,动画可能就不起作用了。相反,您可以保留一些类并删除其余类,如下所示:

$('#container div.cleanstate').removeClass('removethis removethat').addClass('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');
失而复得 2024-11-01 08:30:35

关于 robs 答案,为了完整起见,您还可以将 querySelector 与 vanilla 一起使用

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

regarding to robs answer and for and for the sake of completeness you can also use querySelector with vanilla

document.querySelector('#container div.cleanstate').className = "cleanstate";
呢古 2024-11-01 08:30:35

如果您想保留一门或多于一门课程并且想要除这些课程之外的课程怎么办?如果您不想删除所有类并再次添加该特定类,则这些解决方案将不起作用。
使用attr和removeClass()会重置第一个实例中的所有类,然后再次附加该特定类。如果您在再次重置的类上使用一些动画,它将失败。

如果您想简单地删除除某些类之外的所有类,那么这适合您。
我的解决方案是: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;
        };

这不会重置所有类,它只会删除必要的类。

我在我的项目中需要它,我只需要删除不匹配的类。

您可以使用它 $(".third").removeClassesExceptThese(["first", "second"]);

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

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"]);

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文