使用if语句检查变量true [英] Check variable true with if statement
问题描述
这是我的代码:
$(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();
}
});
它不符合 flag = true
条件。如果我 alert
在 $(val1).mouseleave(function(){});
中,则显示 flag = true
;但是当我在 $(val1).mouseleave(function(){});
之外提醒它时,它显示 flag = false
。
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
.
好吧,让我解释一下:我有4个街区 val1,val2,val3
和 VAL4
。当用户离开 val1
并输入 val3
或 val4
阻止,我想设置/添加不透明度等级
...如果他们不输入
进入 val3 / val4
但进入进入 val2或其他块
块然后我想删除不透明度类。
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 go into val2 or another block
block then I want to remove opacity class.
推荐答案
在您进行if测试时:
if(flag==true)
... flag
总是为false,因为您在此之前将其设置为 false
。它被设置为 true
的唯一位置是你绑定的 mouseenter
处理程序,但处理程序函数没有在那一点上被调用。
...flag
will always be 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.
让我在代码的开头添加一些注释,试图让它更清晰:
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
从 mouseleave
处理程序中绑定 mouseenter
处理程序是没有意义的,因为表示每次发生mouseleave时都绑定一个额外的 mousee nter
相同元素的处理程序。
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
代码。)
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.)
这篇关于使用if语句检查变量true的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!