问题描述
我的类元素可能是这样的:
I have elements with classes that can be like this:
class="refType indent_00"
class="refType indent_01"
class="refType indent_02"
..
class="refType indent_10"
是否有一种简便的方法可以从其中删除index_xx类?
Is there an easy way that I can remove the index_xx class from these?
谢谢
推荐答案
如果您可能在index_xx
类名称上找到的所有对象上也都有refType
类,则可以执行以下操作:
If all the objects you might find the index_xx
class name on also have the refType
class on them, then you can do this:
$(".refType").each(function() {
this.className = $.trim(this.className.replace(/(^|\s)indent_\d+($|\s)/, " "));
});
如果它们都不都具有refType
类,则可以执行以下操作:
If, they don't all have the refType
class, then you can do this:
$("[class*='indent_']").each(function() {
this.className = $.trim(this.className.replace(/(^|\s)indent_\d+($|\s)/, " "));
});
或者,使用所有jQuery,您都可以这样做:
Or, using all jQuery, you can do this:
$("[class*='indent_']").removeClass(function(i, cls) {
var retVal = "";
var matches = cls.match(/(^|\s)(indent_\d+)($|\s)/);
if (matches) {
retVal = matches[2];
}
return(retVal);
});
第一个可能更有效.如果您可以将此范围限定在DOM的某些部分而不是整个DOM上,那么这可能会提高性能.
The first one is probably more efficient. If you can scope this to some part of the DOM rather than the entire DOM, then that may help performance.
此处的第二个示例: http://jsfiddle.net/jfriend00/PkXag/
这篇关于如何使用基于类名称的某些字符的jQuery从元素中删除类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!