【发布时间】:2018-11-21 06:46:25
【问题描述】:
我正在尝试使用布尔值来处理一些 if 语句。 我的代码如下所示:
let found = false
function myFunction() {
if (found === false) {
($('#character').click(function() {
$('#redBox').animate({
right: '-=700px'
});
$("#redBox").css({
backgroundColor: "grey"
});
$('#blueBox').animate({
right: '-=700px'
});
$("#blueBox").css({
backgroundColor: "grey"
});
found = true;
}));
};
}
}
myFunction();
if (found === false) {
($('#redBox').click(function() {
$('#character').animate({
right: '-=700px'
});
$("#character").css({
backgroundColor: "grey"
});
$('#blueBox').animate({
right: '-=700px'
});
$("#blueBox").css({
backgroundColor: "grey"
});
found = true;
}));
};
}
我想要发生的是只运行一个或另一个 if 语句。 因此,当我按下一个 div(即#character)时,布尔值变为真,防止另一个 if 语句运行。
然而,这并没有发生,即使布尔值变为真,未单击的 if 语句仍会运行。
感谢我能得到的任何帮助!
【问题讨论】:
-
你有一些额外的
}字符。 -
为什么你的
$(...).click()函数有括号?
标签: javascript jquery html if-statement boolean