无法在 JavaScript 中创建多个 if 条件?

发布于 2024-11-24 03:07:53 字数 259 浏览 1 评论 0原文

我完全不知道为什么这不起作用。对我来说毫无意义。

这会返回“语法错误:解析错误”:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

裸钻 2024-12-01 03:07:53

存在三种不同的运算符:

  • =:赋值
  • ==:相等
  • ===:严格相等

= 实际修改变量,因此您不应在 if 语句中使用它。也就是说,您应该使用 ... || opening == true) 而不是 ... ||开放=真)

There are three different operators at play:

  • =: assignment
  • ==: equality
  • ===: strict equality

= actually modifies a variable, so you shouldn't use it inside if statements. That is, you should use ... || opening == true) instead of ... || opening = true).

心意如水 2024-12-01 03:07:53

在 JavaScript 中,= 用于赋值,而 ===== 用于比较它们。

当您将 opening = true 放入 if 语句时,您不会检查 opening 是否为 true,而是设置 opening< /code> 更改为 true。尝试使用 == 代替。

例如,

var x = 5;
if (x == 10) {
    alert("x is 10");
} else {
    alert("x isn't 10");
}

将显示“x is not 10”,而

var x = 5;
if (x = 10) {
    alert("x is 10");
} else {
    alert("x isn't 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 if opening is true, you are setting opening to true. Try using == instead.

For example,

var x = 5;
if (x == 10) {
    alert("x is 10");
} else {
    alert("x isn't 10");
}

will display "x isn't 10", while

var x = 5;
if (x = 10) {
    alert("x is 10");
} else {
    alert("x isn't 10");
}

will display "x is 10".

瑶笙 2024-12-01 03:07:53

第一个示例应为:

if ($(this).attr("id") === 'search' || opening == true) return false;

第二个示例:

if (1 == 1 && 2 == 2) { return false; }

请注意,逻辑等于的双等号 (==) 与单等号 (=) 不同,即处理变量赋值。

the first example should read:

if ($(this).attr("id") === 'search' || opening == true) return false;

and the second:

if (1 == 1 && 2 == 2) { return false; }

Note the double equals (==) sign for logic equals is not the same as the single equals (=) sign, that handles variable assignment.

星星的軌跡 2024-12-01 03:07:53

您的条件有错误,

if ($(this).attr("id") === 'search' || opening = true) return false;

问题应该

if ($(this).attr("id") === 'search' || opening == true) return false;

是等号

=== 不同,

第一个是赋值运算符。第二个是为了比较

You have an error in your condition

if ($(this).attr("id") === 'search' || opening = true) return false;

should be

if ($(this).attr("id") === 'search' || opening == true) return false;

the problem is with the equals sign

= is different to ==

the first one is the assignment operator. the second one is for comparison

∝单色的世界 2024-12-01 03:07:53

当您这样测试时:

opening=true;

您真正要做的是将 opening 设置为 true 的值。使用 == 代替。

最后,即使操作顺序正确,也可能会造成混乱。将比较的每个部分放在括号中。

if (($(this).attr("id") === 'search') || (opening == true)) return false;

When you test like this:

opening=true;

What you are really doing is setting opening to the value of true. 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;
熊抱啵儿 2024-12-01 03:07:53

我的猜测是 === 不存在。
== 用于测试相等性

,因此 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;

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文