jQuery hasClass() - 检查多个类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2214952/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 13:02:01  来源:igfitidea点击:

jQuery hasClass() - check for more than one class

jqueryperformancejquery-selectors

提问by Hans

With:

和:

if(element.hasClass("class"))

I can check for one class, but is there an easy way to check whether "element" has any of many classes?

我可以检查一个类,但是有没有一种简单的方法来检查“元素”是否有多个类中的任何一个?

I am using:

我在用:

if(element.hasClass("class") || element.hasClass("class") ... )

Which isn't too bad, but I am thinking of something like:

这还不错,但我在想:

if(element.hasClass("class", "class2")

Which unfortunately doesn't work.

不幸的是,这不起作用。

Is there something like that?

有这样的事情吗?

回答by Simon Arnold

element.is('.class1, .class2')

works, butit's 35% slowerthan

的作品,它是35%慢

element.hasClass('class1') || element.hasClass('class2')

enter image description here

在此处输入图片说明

If you doubt what i say, you can verify on jsperf.com.

如果你怀疑我所说的,你可以在jsperf.com验证

Hope this help someone.

希望这有助于某人。

回答by Matchu

How about:

怎么样:

element.is('.class1, .class2')

回答by Kalel Wade

$.fn.extend({
    hasClasses: function (selectors) {
        var self = this;
        for (var i in selectors) {
            if ($(self).hasClass(selectors[i])) 
                return true;
        }
        return false;
    }
});

$('#element').hasClasses(['class1', 'class2', 'class3']);

This should do it, simple and easy.

这应该可以做到,简单易行。

回答by John Magnolia

filter()is another option

filter()是另一种选择

Reduce the set of matched elements to those that match the selector or pass the function's test.

将匹配元素集减少到与选择器匹配或通过函数测试的元素集。

$(selector).filter('.class1, .class2'); //Filter elements: class1 OR class2

$(selector).filter('.class1.class2'); // Filter elements: class1 AND class2

回答by Robert ?oziński

How about this?

这个怎么样?

if (element.hasClass("class1 class2")

回答by Henrik Myntti

here's an answer that does follow the syntax of

这是一个确实遵循以下语法的答案

$(element).hasAnyOfClasses("class1","class2","class3")
(function($){
    $.fn.hasAnyOfClasses = function(){
        for(var i= 0, il=arguments.length; i<il; i++){
            if($self.hasClass(arguments[i])) return true;
        }
        return false;
    }
})(jQuery);

it's not the fastest, but its unambiguous and the solution i prefer. bench: http://jsperf.com/hasclasstest/10

它不是最快的,但它的明确性和我更喜欢的解决方案。板凳:http: //jsperf.com/hasclasstest/10

回答by Okan Kocyigit

What about this,

那这个呢,

$.fn.extend({
     hasClasses: function( selector ) {
        var classNamesRegex = new RegExp("( " + selector.replace(/ +/g,"").replace(/,/g, " | ") + " )"),
            rclass = /[\n\t\r]/g,
            i = 0,
            l = this.length;
        for ( ; i < l; i++ ) {
            if ( this[i].nodeType === 1 && classNamesRegex.test((" " + this[i].className + " ").replace(rclass, " "))) {
                return true;
            }
        }
        return false;
    }
});

Easy to use,

便于使用,

if ( $("selector").hasClasses("class1, class2, class3") ) {
  //Yes It does
}

And It seems to be faster, http://jsperf.com/hasclasstest/7

而且似乎更快, http://jsperf.com/hasclasstest/7

回答by u01jmg3

What about:

关于什么:

if($('.class.class2.class3').length > 0){
    //...
}

回答by Rangel R. Morais

Works for me:

对我有用:

 if ( $("element").hasClass( "class1") || $("element").hasClass("class2") ) {

 //do something here

 }

回答by Hamidreza

You can do this way:

你可以这样做:

if($(selector).filter('.class1, .class2').length){
    // Or logic
}

if($(selector).filter('.class1, .class2').length){
    // And logic
}