Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何根据类名的某些字符使用jQuery从元素中删除类?_Jquery - Fatal编程技术网

如何根据类名的某些字符使用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
类,而不考虑数量。