无法在 JavaScript 中创建多个 if 条件?
我完全不知道为什么这不起作用。对我来说毫无意义。
这会返回“语法错误:解析错误”:
if ($(this).attr("id") === 'search' || opening = true) return false;
为了更好地衡量,我还尝试了以下操作,得到了相同的结果:
if (1 = 1 && 2 = 2) { return false; }
I have absolutely no idea why this is not working. Makes no sense to me.
This returns a "syntax error: parse error":
if ($(this).attr("id") === 'search' || opening = true) return false;
For good measure, I also tried the following, which yielded the same result:
if (1 = 1 && 2 = 2) { return false; }
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
存在三种不同的运算符:
=
:赋值==
:相等===
:严格相等=
实际修改变量,因此您不应在if
语句中使用它。也就是说,您应该使用... || opening == true)
而不是... ||开放=真)
。There are three different operators at play:
=
: assignment==
: equality===
: strict equality=
actually modifies a variable, so you shouldn't use it insideif
statements. That is, you should use... || opening == true)
instead of... || opening = true)
.在 JavaScript 中,
=
用于赋值,而==
和===
用于比较它们。当您将
opening = true
放入 if 语句时,您不会检查opening
是否为true
,而是设置opening< /code> 更改为
true
。尝试使用==
代替。例如,
将显示“x is not 10”,而
将显示“x is 10”。
In JavaScript
=
is used to assign values, while==
and===
are used to compare them.When you put
opening = true
in your if statement, you aren't checking ifopening
istrue
, you are settingopening
totrue
. Try using==
instead.For example,
will display "x isn't 10", while
will display "x is 10".
第一个示例应为:
第二个示例:
请注意,逻辑等于的双等号 (
==
) 与单等号 (=
) 不同,即处理变量赋值。the first example should read:
and the second:
Note the double equals (
==
) sign for logic equals is not the same as the single equals (=
) sign, that handles variable assignment.您的条件有错误,
问题应该
是等号
=
与==
不同,第一个是赋值运算符。第二个是为了比较
You have an error in your condition
should be
the problem is with the equals sign
=
is different to==
the first one is the assignment operator. the second one is for comparison
当您这样测试时:
您真正要做的是将
opening
设置为true
的值。使用==
代替。最后,即使操作顺序正确,也可能会造成混乱。将比较的每个部分放在括号中。
When you test like this:
What you are really doing is setting
opening
to the value oftrue
. Use==
instead.Finally, order of operations, even if correct, can get confusing. Put parenthesis around each part of the comparison.
我的猜测是 === 不存在。
== 用于测试相等性
,因此
if ($(this).attr("id") === 'search' || opening == true) return false;
应该是
if ($(this).attr("id") == 'search' || opening == true) return false;
My guess is that === does not exist.
== is for testing equality
so
if ($(this).attr("id") === 'search' || opening == true) return false;
should be
if ($(this).attr("id") == 'search' || opening == true) return false;