0

I have a list of spans with a variety of classes. I want to check if a given span has the same class as another element.

if ( $(this).attr('class') === getClass.match('Rock, Paper, Scissors') ) {
    //do stuff
}

.match doesn't work with a comma separated list. I was wondering how I would do this then.

4

3 回答 3

6

Why not just

$(this).is('.Rock, .Paper, .Scissors')
于 2013-06-23T01:23:08.893 回答
1

Using a pipe separated regex should work.

/A|B|C/

http://www.w3schools.com/jsref/jsref_regexp_test.asp

Here is the documentation for the test method which will return true/false directly.

 var str="Hello world!";
 //look for "Hello" or "world"
 var patt=/Hello|world/g;
 var result=patt.test(str);
于 2013-06-23T01:21:07.423 回答
0

我建议查看该filter方法 ( http://api.jquery.com/filter/ ),因为您的元素可能有多个类,在这种情况下,您发布的代码可能不会产生预期的结果。您可以filter按如下方式使用该功能:

$(this).filter('.Rock, .Paper, .Scissors')

所以你可以像下面这样使用它:

$.each($(element).filter('.Rock, .Paper, .Scissors'), function(index, value) {
    // do stuff
});
于 2013-06-23T01:33:07.767 回答