Javascript 如何在is()方法中使用多个变量(选择器)

Javascript 如何在is()方法中使用多个变量(选择器),javascript,jquery,Javascript,Jquery,甚至可以使用Is()检查多个选择器作为变量吗?您可以使用此选择器 var container = $(".container"), background = $(".background"); // ... // Inside an each() method – check if is container OR background if (this.is(container, background)) { // Not working // do something

甚至可以使用Is()检查多个选择器作为变量吗?

您可以使用此选择器

var container   = $(".container"),
    background  = $(".background");

// ...

// Inside an each() method – check if is container OR background
if (this.is(container, background)) { // Not working
    // do something
}

// I know I could do it like this ...
if (this.is(container) || this.is(background)) {
    // do something
}

// ... but I want it shorter
所以基本上, 1)

this.is('.container, .background')
2)

3)

4)

5)


是的,我可以这样做,但我需要/想要变量;)另一种可能的方法,是的。所以不可能直接检查is()方法中的多个变量@本:更新后的第三种方法解决了你的问题吗?@本:我测试了第三种方法,对我有效。。。这不是你想要的东西吗?@Ben:有更多的方法适合你……;)
if (this.is('.container, .background')) {
    // do something
}
var elements = ('.container, .background');
if (this.is(elements))  {
}
if (this.is([container[0], background[0]])) {
    // do something
}
if (this.is(container.add(background))) {
    // do something
}
var temp = container.add(background);
if (this.is(temp)) {
    // do something
}