简体   繁体   English

使用JQuery removeClass()删除除一个之外的所有类

[英]Using JQuery removeClass() to remove all classes but one

Okay, I've got an interesting one (well, interesting to me, anyway :) ). 好的,我有一个有趣的(好吧,对我来说很有趣,反正:))。

I've got a situation where I have a div with a static class value, but it also can have a single, "secondary class" assigned that is dynamic. 我有一个情况,我有一个静态类值的div,但它也可以分配一个动态的“二级类”。 When the user makes a selection, any existing secondary class needs to be removed and the new class added. 当用户进行选择时,需要删除任何现有的辅助类并添加新类。

Ignoring using an id value (standards for the project use the class . . . can't be changed), is there an elegant way to simply ignore the first class and remove whatever other class is there, before adding the new one? 忽略使用id值(项目的标准使用类......无法更改),是否有一种优雅的方法可以在添加新类之前简单地忽略第一个类并删除其他任何类?

Example Starting HTML: 示例启动HTML:

<div class="staticClass dynaClass1" />

Example JS: 示例JS:

function updateClass(newSecondaryClass) {
    $(".staticClass") . . . **** remove any class besides "staticClass" ****
    $(".staticClass").addClass(newSecondaryClass);
}

If the function is called using updateClass("dynaClass2"); 如果使用updateClass("dynaClass2");调用该函数updateClass("dynaClass2"); , the resulting HTML should be: ,生成的HTML应该是:

<div class="staticClass dynaClass2" />

I can think of ways of doing it involving just removing all classes using removeClass(); 我可以想办法,只需使用removeClass();删除所有类removeClass(); and adding "staticClass" back in when adding the new class, or using attr("class", "staticClass " + newSecondaryClass); 并在添加新类时添加“staticClass”,或者使用attr("class", "staticClass " + newSecondaryClass); , but I'm wondering if there isn't a way to handle it without having to touch the static class at all? ,但我想知道是否有办法处理它而不必触及静态类?

In the end, I guess this is an academic question, more than anything . 最后,我想这是一个学术问题,比什么都重要。 . . just seems like it's something that should be doable, but I don't know how to do it. 看起来它应该是可行的,但我不知道该怎么做。 :D :d

You can remove all classes and add the one you want to leave: 您可以删除所有类并添加要留下的类:

$(".staticClass").removeClass().addClass('staticClass');

Calling removeClass() without a parameter removes all classes. 在没有参数的情况下调用removeClass()会删除所有类。

If you don't want to do that then you can simply modify the class attribute: 如果您不想这样做,那么您只需修改class属性:

$(".staticClass").attr('class', 'staticClass');

You can pass a function to remove class, which returns all but the static Classes: 您可以传递一个函数来删除类,它返回除静态类之外的所有类:

$('.staticClass').removeClass(function(index, klass) { 
  return klass.replace(/(^|\s)+staticClass\s+/, ''); 
})

This is returning all the classes that are on the object, without the static one, and therefore removes all classes but the static one. 这将返回对象上的所有类,而不返回静态类,因此删除除静态类之外的所有类。

Pass a function to .removeClass() 将函数传递给.removeClass()

A revision of Beat Richartz's answer on this page. Beat Richartz在此页面上的答案修订版。

Note: I tried to post this as an edit and it was rejected. 注意:我尝试将此作为编辑发布,但它被拒绝了。 The concept is identical, with an improved RegEx. 这个概念是相同的,具有改进的RegEx。

Improved RegEx provides word-boundary matching with multiple classes 改进的RegEx提供了与多个类的字边界匹配

// Remove all classes except those specified
$('span').removeClass(function () { 
    return $(this).attr('class').replace(/\b(?:hello|world)\b\s*/g, ''); 
});

Before: 之前:

<span class="hello foo">hello</span> <span class="world bar">world</span>`

After: 后:

<span class="hello">hello</span> <span class="world">world</span>`

Try it: http://jsfiddle.net/gfullam/52eeK/3/ 试一试: http //jsfiddle.net/gfullam/52eeK/3/

Try it as a jQuery plugin: http://jsfiddle.net/gfullam/52eeK/5/ 尝试将其作为jQuery插件: http //jsfiddle.net/gfullam/52eeK/5/

FWIW: This method is necessary when you don't want to replace the existing classes with other classes as in .attr('class', '<final list of classes>') , but instead just want to remove those that don't match a list of classes. FWIW:当您不想将现有类替换为.attr('class', '<final list of classes>')其他类时,此方法是必需的,而只是想删除那些不存在的类。匹配一个类列表。

You can set it's required classes using the .attr() function. 您可以使用.attr()函数设置它所需的类。 So: 所以:

$('.staticClass').attr('class','<any classes required');

This will replace any classes that were originally there, and add the new ones. 这将替换原来的任何类,并添加新的类。

All of your classes are manipulated by calling the Javascript DOM element.className , so basically jQuery's addClass just replaces that string. 所有类都是通过调用Javascript DOM element.className来操作的,所以基本上jQuery的addClass只是替换了那个字符串。 You can see that in the Github source . 你可以在Github源代码中看到它。

Which all means that if you call 这一切都意味着,如果你打电话

$('.staticClass').addClass('someClass');

The element.className is replaced anyway, but with the new class included. 无论如何都会替换element.className ,但包含新类。 ( this means that your staticClass is actually touched :) (这意味着你的staticClass实际上staticClass被触及:)

When you call 你打电话的时候

$('.staticClass').removeClass().addClass('staticClass');

you will replace that string twice and there is no problem doing that. 你将两次替换该字符串,这样做没有问题。

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

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