有没有什么简单的方法可以删除所有匹配的类,例如,
color-*
所以如果我有一个元素:
<div id="hello" class="color-red color-brown foo bar"></div>
删除后,它将是
<div id="hello" class="foo bar"></div>
谢谢!
发布于 2011-03-03 22:35:44
从jQuery 1.4开始,removeClass函数接受一个函数参数。
$("#hello").removeClass (function (index, className) {
return (className.match (/(^|\s)color-\S+/g) || []).join(' ');
});
现场示例:http://jsfiddle.net/xa9xS/1409/
发布于 2010-04-15 18:13:24
$('div').attr('class', function(i, c){
return c.replace(/(^|\s)color-\S+/g, '');
});
发布于 2011-12-30 22:04:22
我已经写了一个插件,叫做alterClass --使用通配符匹配删除元素类。可选地添加类:https://gist.github.com/1517285
$( '#foo' ).alterClass( 'foo-* bar-*', 'foobar' )
https://stackoverflow.com/questions/2644299
复制相似问题