jQuery还是选择器?
只是想弄清楚如何在一段现有代码中测试多个选择器,例如jQuery还是选择器?,jquery,jquery-selectors,if-statement,Jquery,Jquery Selectors,If Statement,只是想弄清楚如何在一段现有代码中测试多个选择器,例如 if (!(jQuery('#selector1 || #selector2').hasClass('some-class'))) { //code here } 这不起作用,但我想知道我是否可以做些什么来让它起作用?只需选择它们两个,如果其中任何一个都有类某个类,则条件为真: if (!jQuery('#selector1, #selector2').hasClass('some-class')) { //code here }
if (!(jQuery('#selector1 || #selector2').hasClass('some-class'))) {
//code here
}
这不起作用,但我想知道我是否可以做些什么来让它起作用?只需选择它们两个,如果其中任何一个都有类
某个类
,则条件为真:
if (!jQuery('#selector1, #selector2').hasClass('some-class')) {
//code here
}
或者,如果我曲解了你的逻辑,你可以(并且应该,为了速度和简单起见)将它们分成两部分:
if (!(jQuery('#selector1').hasClass('some-class') || jQuery('#selector2').hasClass('some-class'))) {
//code here
}
为什么不这样呢?很简单,是个逗号;)
据我所知没有。也许你可以这样做:
if((!$('#selectorid1').hasClass(class)) || (!$('#selectorid2').hasClass(class))){}
非常感谢-是的,你添加的第二段代码就是我想要的。thanks@hunter-您的答案不正确,您正在检查子体上的类(效率要低得多)。
if (!(jQuery('#selector1 .some-class, #selector2 .some-class'))) {
//code here
}
if((!$('#selectorid1').hasClass(class)) || (!$('#selectorid2').hasClass(class))){}