如何根据类名的某些字符使用jQuery从元素中删除类?
我有一些元素的类可以是这样的:如何根据类名的某些字符使用jQuery从元素中删除类?,jquery,Jquery,我有一些元素的类可以是这样的: class="refType indent_00" class="refType indent_01" class="refType indent_02" .. class="refType indent_10" 有没有一种简单的方法可以从这些类中删除index_xx类 感谢使用;e、 g: 另请参见。如果您可能在其上找到索引\u xx类名的所有对象上也有refType类,则可以执行以下操作: $(".refType").each(function() {
class="refType indent_00"
class="refType indent_01"
class="refType indent_02"
..
class="refType indent_10"
有没有一种简单的方法可以从这些类中删除index_xx类
感谢使用;e、 g:
另请参见。如果您可能在其上找到
索引\u xx
类名的所有对象上也有refType
类,则可以执行以下操作:
$(".refType").each(function() {
this.className = $.trim(this.className.replace(/(^|\s)indent_\d+($|\s)/, " "));
});
$("[class*='indent_']").each(function() {
this.className = $.trim(this.className.replace(/(^|\s)indent_\d+($|\s)/, " "));
});
$("[class*='indent_']").removeClass(function(i, cls) {
var retVal = "";
var matches = cls.match(/(^|\s)(indent_\d+)($|\s)/);
if (matches) {
retVal = matches[2];
}
return(retVal);
});
如果它们不都具有refType
类,则可以执行以下操作:
$(".refType").each(function() {
this.className = $.trim(this.className.replace(/(^|\s)indent_\d+($|\s)/, " "));
});
$("[class*='indent_']").each(function() {
this.className = $.trim(this.className.replace(/(^|\s)indent_\d+($|\s)/, " "));
});
$("[class*='indent_']").removeClass(function(i, cls) {
var retVal = "";
var matches = cls.match(/(^|\s)(indent_\d+)($|\s)/);
if (matches) {
retVal = matches[2];
}
return(retVal);
});
或者,使用所有jQuery,您可以执行以下操作:
$(".refType").each(function() {
this.className = $.trim(this.className.replace(/(^|\s)indent_\d+($|\s)/, " "));
});
$("[class*='indent_']").each(function() {
this.className = $.trim(this.className.replace(/(^|\s)indent_\d+($|\s)/, " "));
});
$("[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,那么这可能有助于提高性能
这里的第二个工作示例:我想问题是如何删除所有
indent_xx
类,而不考虑数量。