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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 08:16:18  来源:igfitidea点击:

Check variable true with if statement

javascriptjqueryjquery-pluginsif-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 = truecondition. If I alertwithin $(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,val3and val4. When the users leave val1and enter the val3or val4block, I want to set/add opacity class... if they do not enterinto val3/val4but gointo val2 or another blockblock then I want to remove opacity class.

好吧,让我解释一下:我有 4 个块val1,val2,val3val4. 当用户离开val1,并进入val3val4块,我想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)

...flagwill alwaysbe false, because you set it to falsejust before that. The only place it gets set to trueis inside the mouseenterhandler that you bind there but that handler function doesn't get called at that point.

...flag始终为 false,因为您在此false之前将其设置为。它被设置到的唯一位置truemouseenter您绑定在那里的处理程序内部,但该处理程序函数在那时不会被调用。

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 mouseenterhandler from inside the mouseleavehandler, because that means every time the mouseleave occurs you bind an additional mouseenterhandler 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 mouseentercode 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";
});