如何控制哪部分&&在if语句中应用

how to control which parts the && applies to in an if statement

本文关键字:if 语句 应用 哪部 何控制 控制      更新时间:2023-09-26

假设我有这样一个if语句:

if($(this).hasClass('rightnav') | $(this).parent() == 'nav' && $(this).parent().parent() == 'section')

如何告诉脚本&&应该应用在|之后,而不是整个if?谢谢!s

if(($(this).hasClass('rightnav') | $(this).parent() == 'nav') && $(this).parent().parent() == 'section')

应该是||而不是|吗?

注意,您应该使用||(逻辑或)而不是|(位或)。

第二,逻辑-and (&&)比逻辑-and (||)具有更高的优先级。参见以下优先级列表中的数字13和14:https://developer.mozilla.org/en/JavaScript/Reference/operators/operator_precedence

但是如果你不确定,或者为了帮助以后的维护程序员,括号是一个很好的选择。

我想这个应该可以了:

if(($(this).hasClass('rightnav') || $(this).parent() == 'nav') && $(this).parent().parent() == 'section')