如何使用 javascript OR 运算符?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4398221/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use javascript OR operator?
提问by William Lekatompessy
I've been breaking my head over this 'simple' javascript snippet:
我一直在为这个“简单”的 javascript 代码片段烦恼:
$("#hitbox").mouseleave(function() {
if($("#sub-wrapper-1").height() < 179 || $("#sub-wrapper-2").height() < 179 ) {
$("#hitbox").animate({ "height" : '0px' }, 800),
} else {
$("#hitbox").stop();
}
});
Could you guys nudge me in the right direction on how to use the OR
operator in javascript? The above code doesn't throw any errors, but it seems as it runs the function as an AND
operator.
你们能否就如何OR
在 javascript 中使用运算符向我提出正确的方向?上面的代码没有抛出任何错误,但它似乎是作为AND
操作符运行函数的。
回答by Mauro
Your OR operator is right (in fact in your case is simple javascript!)
您的 OR 运算符是正确的(实际上在您的情况下是简单的 javascript!)
It seems you have an error here
看来你这里有错误
$("#hitbox").animate({ "height" : '0px' },800),
fix it as
将其修复为
$("#hitbox").animate({ height : '0px' },800);
回答by gordon
I find sometimes JS operators (in this case your || or) don't compare whole clauses, only the next item starting the clause. you may want to try wrapping your clauses w/ parens like so:
我发现有时 JS 运算符(在这种情况下是您的 || 或)不比较整个子句,只比较开始子句的下一项。你可能想尝试用括号包裹你的子句,如下所示:
if(($("#sub-wrapper-1").height() < 179) || ($("#sub-wrapper-2").height() < 179 ))
if(($("#sub-wrapper-1").height() < 179) || ($("#sub-wrapper-2").height() < 179))