jQuery removeClass通配符

atp*_*atp 354 html jquery

有没有简单的方法来删除所有匹配的类,例如,

color-*
Run Code Online (Sandbox Code Playgroud)

所以,如果我有一个元素:

<div id="hello" class="color-red color-brown foo bar"></div>
Run Code Online (Sandbox Code Playgroud)

删除后,它会

<div id="hello" class="foo bar"></div>
Run Code Online (Sandbox Code Playgroud)

谢谢!

jim*_*mig 648

removeClass功能需要,因为函数参数的jQuery 1.4.

$("#hello").removeClass (function (index, className) {
    return (className.match (/(^|\s)color-\S+/g) || []).join(' ');
});
Run Code Online (Sandbox Code Playgroud)

实例:http://jsfiddle.net/xa9xS/1409/

  • 我认为值得注意的是,这将捕获一开始没有开始的匹配类的前导空格.Javascript不支持正面的lookbehind,因此您必须使用捕获组解决方法.然而,这是一个没有实际意义,因为removeClass函数将通过`classes =(value ||"").匹配(rnotwhite)||从您的类字符串中删除空格.[];` (3认同)
  • @lowtechsun你可以添加你的正则表达式`(color- | sport- | nav-)`.它将匹配`color -`,或`sport -`或`nav -`.所以,上面的答案将成为`/(^ |\s)(color- | sport- | nav - )\ S +/g`. (3认同)
  • @CarlEdman你的提议也会匹配"foo-magic1". (3认同)
  • 接受的答案是正确的,但习惯上,我会使用匹配任何单词边界的 \b 而不是更冗长的 (^|\s)。因此,例如,要删除由单词 magic 后跟一个非负整数组成的所有类,并且只有那些类,我会匹配 /\bmagic\d+\b/。 (2认同)

Kob*_*obi 97

$('div').attr('class', function(i, c){
    return c.replace(/(^|\s)color-\S+/g, '');
});
Run Code Online (Sandbox Code Playgroud)

  • 我喜欢这个,因为它减少了开销并且直截了当.为什么在attr做得更好的时候使用remove class? (5认同)
  • 我认为你需要防止空类(`c未定义')?至少当我在这个页面上尝试[在我的插件下面](http://stackoverflow.com/a/19059585/1037948)时,`$('a').stripClass('post',1)`throw" TypeError:无法调用未定义的方法'replace' (2认同)
  • 添加一个快速测试对我有用:`return c &amp;&amp; c.replace(/\bcolor-\S+/g, '');` (2认同)

Pet*_*e B 50

我编写了一个名为alterClass的插件 - 使用通配符匹配删除元素类.可选择添加类:https://gist.github.com/1517285

$( '#foo' ).alterClass( 'foo-* bar-*', 'foobar' )
Run Code Online (Sandbox Code Playgroud)


tre*_*mby 15

我将它概括为一个Jquery插件,它以正则表达式作为参数.

咖啡:

$.fn.removeClassRegex = (regex) ->
  $(@).removeClass (index, classes) ->
    classes.split(/\s+/).filter (c) ->
      regex.test c
    .join ' '
Run Code Online (Sandbox Code Playgroud)

使用Javascript:

$.fn.removeClassRegex = function(regex) {
  return $(this).removeClass(function(index, classes) {
    return classes.split(/\s+/).filter(function(c) {
      return regex.test(c);
    }).join(' ');
  });
};
Run Code Online (Sandbox Code Playgroud)

因此,对于这种情况,使用将是(Coffee和Javascript):

$('#hello').removeClassRegex(/^color-/)
Run Code Online (Sandbox Code Playgroud)

请注意,我正在使用Array.filterIE <9中不存在的功能.您可以使用Underscore的过滤功能,也可以使用谷歌作为此WTFPL之类的polyfill.


小智 10

如果你想在其他地方使用它我建议你扩展.这个对我来说很好.

 $.fn.removeClassStartingWith = function (filter) {
    $(this).removeClass(function (index, className) {
        return (className.match(new RegExp("\\S*" + filter + "\\S*", 'g')) || []).join(' ')
    });
    return this;
};
Run Code Online (Sandbox Code Playgroud)

用法:

$(".myClass").removeClassStartingWith('color');
Run Code Online (Sandbox Code Playgroud)


ARS*_*S81 5

对于 jQuery 插件试试这个

$.fn.removeClassLike = function(name) {
    return this.removeClass(function(index, css) {
        return (css.match(new RegExp('\\b(' + name + '\\S*)\\b', 'g')) || []).join(' ');
    });
};
Run Code Online (Sandbox Code Playgroud)

或这个

$.fn.removeClassLike = function(name) {
    var classes = this.attr('class');
    if (classes) {
        classes = classes.replace(new RegExp('\\b' + name + '\\S*\\s?', 'g'), '').trim();
        classes ? this.attr('class', classes) : this.removeAttr('class');
    }
    return this;
};
Run Code Online (Sandbox Code Playgroud)

编辑:第二种方法应该更快一点,因为它只对整个类字符串运行一个正则表达式替换。第一个(较短)使用 jQuery 自己的removeClass 方法,该方法迭代所有现有的类名并针对给定的正则表达式逐一测试它们,因此在幕后它为同一工作执行了更多步骤。但在现实生活中,这种差异可以忽略不计。

速度比较基准


Fif*_*ifi 5

删除任何以 开头的类的通用函数begin

function removeClassStartingWith(node, begin) {
    node.removeClass (function (index, className) {
        return (className.match ( new RegExp("\\b"+begin+"\\S+", "g") ) || []).join(' ');
    });
}
Run Code Online (Sandbox Code Playgroud)

http://jsfiddle.net/xa9xS/2900/

function removeClassStartingWith(node, begin) {
    node.removeClass (function (index, className) {
        return (className.match ( new RegExp("\\b"+begin+"\\S+", "g") ) || []).join(' ');
    });
}
Run Code Online (Sandbox Code Playgroud)
var begin = 'color-';

function removeClassStartingWith(node, begin) {
    node.removeClass (function (index, className) {
        return (className.match ( new RegExp("\\b"+begin+"\\S+", "g") ) || []).join(' ');
    });
}

removeClassStartingWith($('#hello'), 'color-');

console.log($("#hello")[0].className);
Run Code Online (Sandbox Code Playgroud)