javascript 使用 if 语句检查变量是否为真
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17424352/
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
Check variable true with if statement
提问by Friend
Here is my code:
这是我的代码:
$(val1).mouseleave(function () {
flag = false;
$(val3 + "," + val4).mouseenter(function () {
flag = true;
//alert(flag);
});
if (flag == true) {
//alert("if"+flag);
$(".big" + i + j + "boxer").show();
$(".big" + i + "box").show();
$(".big" + i + "box").append('<div class="opacity" style="background-color:rgba(00,00,00,0.77);position:absolute;top:0;right:0;left:0;bottom:0;"></div>')
$(".small" + i + "box").append('<div class="opacity" style="background-color:rgba(00,00,00,0.77);position:absolute;top:0;right:0;left:0;bottom:0;"></div>')
} else {
//alert("else"+flag);
$('.opacity').remove();
$(val2).hide();
$(val3).show();
}
});
It's not meeting flag = true
condition. If I alert
within $(val1).mouseleave(function(){});
, it shows that flag = true
; but when I alert it outside of $(val1).mouseleave(function(){});
, it shows flag = false
.
不符合flag = true
条件。如果 Ialert
在 内$(val1).mouseleave(function(){});
,则表明 flag = true
;但是当我在外面提醒它时$(val1).mouseleave(function(){});
,它显示flag = false
.
Well, let me explain: I have 4 blocks val1,val2,val3
and val4
. When the users leave val1
and enter the val3
or val4
block, I want to set/add opacity class
... if they do not enter
into val3/val4
but gointo val2 or another block
block then I want to remove opacity class.
好吧,让我解释一下:我有 4 个块val1,val2,val3
和val4
. 当用户离开val1
,并进入val3
或val4
块,我想set/add opacity class
...如果他们do not enter
进入val3/val4
,但走成val2 or another block
块,然后我想删除不透明类。
回答by nnnnnn
At the point where you do the if test:
在执行 if 测试时:
if(flag==true)
...flag
will alwaysbe false, because you set it to false
just before that. The only place it gets set to true
is inside the mouseenter
handler that you bind there but that handler function doesn't get called at that point.
...flag
将始终为 false,因为您在此false
之前将其设置为。它被设置到的唯一位置true
是mouseenter
您绑定在那里的处理程序内部,但该处理程序函数在那时不会被调用。
Let me add some comments to the beginning of your code to try to make that clearer:
让我在您的代码开头添加一些注释,以使其更清晰:
$(val1).mouseleave(function () {
flag = false; // set flag to false
$(val3 + "," + val4).mouseenter(function () { // bind a mouseenter
flag = true; // that won't be called immediately
//alert(flag); // so won't change flag yet
});
if (flag == true) { // flag is still false
It doesn't make sense to bind a mouseenter
handler from inside the mouseleave
handler, because that means every time the mouseleave occurs you bind an additional mouseenter
handler to the same elements.
mouseenter
从处理程序内部绑定处理程序没有意义mouseleave
,因为这意味着每次发生 mouseleave 时,您都会将额外的mouseenter
处理程序绑定到相同的元素。
I can't really suggest a specific solution because you haven't explained what effect you are trying to achieve. (But I'd probably start by moving that mouseenter
code somewhere else.)
我真的不能建议一个具体的解决方案,因为你还没有解释你想要达到的效果。(但我可能会先将该mouseenter
代码移到其他地方。)
回答by wilsonrufus
$(val1).mouseleave(function () {
flag = false;
$(val3 + "," + val4).mouseenter(function () {
flag = true;
//alert(flag);
if (flag == true) {
//alert("if"+flag);
$(".big" + i + j + "boxer").show();
$(".big" + i + "box").show();
$(".big" + i + "box").append('<div class="opacity" style="background-color:rgba(00,00,00,0.77);position:absolute;top:0;right:0;left:0;bottom:0;"></div>')
$(".small" + i + "box").append('<div class="opacity" style="background-color:rgba(00,00,00,0.77);position:absolute;top:0;right:0;left:0;bottom:0;"></div>')
} else {
//alert("else"+flag);
$('.opacity').remove();
$(val2).hide();
$(val3).show();
}
});
});
回答by mibbler
Since you have clarified your objective, I think something like this would work:
既然你已经阐明了你的目标,我认为这样的事情会奏效:
var haveLeft = null;
$(val1).mouseleave(function () {
haveLeft = "val1";
});
$(val2).mouseenter(function () {
if(haveLeft === "val1") {
// remove opacity class
}
}).mouseleave(function(){
haveLeft = "val2";
});
$(val3 + "," + val4).mouseenter(function () {
if(haveLeft === "val1") {
// add opacity class
}
}).mouseleave(function(){
haveLeft = "val3/4";
});