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
是否可以使用 is() 将多个选择器作为变量进行检查?
最佳答案
您可以使用此选择器
this.is('.container, .background')
所以基本上, 1)
if (this.is('.container, .background')) {
// do something
}
2)
var elements = ('.container, .background');
if (this.is(elements)) {
}
3)
if (this.is([container[0], background[0]])) {
// do something
}
4)
if (this.is(container.add(background))) {
// do something
}
5)
var temp = container.add(background);
if (this.is(temp)) {
// do something
}
关于javascript - 如何通过 is() 方法使用多个变量(选择器),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37482089/