Tom*_*Tom 4 jquery if-statement jquery-selectors
试着弄清楚如何在现有代码中测试多个选择器,就像这样
if (!(jQuery('#selector1 || #selector2').hasClass('some-class'))) {
//code here
}
Run Code Online (Sandbox Code Playgroud)
这不起作用,但想知道我能做些什么才能让它发挥作用?
Tat*_*nen 12
只需选择它们,如果其中任何一个都有类some-class,条件为真:
if (!jQuery('#selector1, #selector2').hasClass('some-class')) {
//code here
}
Run Code Online (Sandbox Code Playgroud)
或者,如果我误解了你的逻辑,你可以(并且为了速度和简单性)将它们分成两部分:
if (!(jQuery('#selector1').hasClass('some-class') || jQuery('#selector2').hasClass('some-class'))) {
//code here
}
Run Code Online (Sandbox Code Playgroud)