简体   繁体   English

JavaScript,如果条件不起作用?

[英]javascript if condition not working?

I am sitting like hours in front of the following code snippet and can not get it running like I want. 我在下面的代码片段前面坐了几个小时,无法按我想要的那样运行。

Basically this code creates a navigation menu on right click, but a click on the switcher should turn off this function and on the next click it gets turned on again. 基本上,这段代码会在右键单击时创建一个导航菜单,但是在切换台上单击将关闭此功能,而在下次单击时,它将再次打开。

Everything works fine (like expected), just the little if statement on line 12 ( if ( switcher % 2 == 0 ) ) does not work like expected, meaning the code within it always gets executed whether var switcher is even or not even. 一切工作正常(如预期),只是第12行上的小if语句(if(switcher%2 == 0))无法按预期工作,这意味着无论var switcher是偶数还是偶数,始终执行其中的代码。 I also tried other conditions like "> 0" and so on but the code within it always gets executed. 我也尝试了其他条件,例如“> 0”,依此类推,但是其中的代码始终会被执行。

$(document).ready(function () {
    /* Set switcher to zero */
    switcher = 0;
    /* If switch gets clicked increment var switcher*/
    $('#guidenavschalter').click(function () {
        switcher++;
        return false;
    });

    /* If var switcher is even execute following code, if not do nothing of this*/
    if (switcher % 2 == 0) {
        /* do not display right click browser menu */
        document.oncontextmenu = function () {
            return false;
        };
        /* if click within #page excluding area of #newid */
        $('#page:not(#newid)').mousedown(function (e) {
            /* if right click */
            if (e.button == 2) {
                /* if #newid already exist display it again */
                if ($('#newid').length) {
                    $('#newid').css({
                        "display": 'block'
                    });
                    $('#newid').css({
                        "top": e.pageY + 'px'
                    });
                    $('#newid').css({
                        "left": e.pageX + 'px'
                    });
                    /* if it does not exist create and display #newid */
                } else {
                    var $div = $('#block-bookoblock-book-outline').clone().attr('id', 'newid');
                    $('body').append($div);
                    $('#newid').css({
                        "top": e.pageY + 'px'
                    });
                    $('#newid').css({
                        "left": e.pageX + 'px'
                    });
                    $('#newid').css({
                        "position": 'absolute'
                    });
                    return false;
                }
            }
            /* if left click hide #newid */
            if (e.button == 0) {
                $('#newid').css({
                    "display": 'none'
                });
            }
            return true;
        });
    }
});

Your code basically is 您的代码基本上是

switcher = 0;

... some irrelevant code here (the callback is not executed right now)

if ( switcher % 2 == 0 ) {

So no wonder the test always pass. 因此,难怪测试总是会通过。

What you probably want is to put the if inside the callback, so that it's tested each time you click : 您可能希望将if放入回调中,以便每次单击时都对其进行测试:

var switcher = 0;
$('#guidenavschalter').click(function(){
    switcher++;
    if ( switcher % 2 == 0 ) {
       ...
    }
    return false;
});
switcher = 0;  // created outside the click event handler

and you're increment the value inside click event handler. 然后在click事件处理程序中增加该值。 Therefore it is always zero. 因此,它始终为零。

You should go through Scoping in JavaScript 您应该在JavaScript中进行范围界定

From comment, you're interested to know more about variable scope in Javascript then 通过评论,您有兴趣进一步了解Javascript中的变量范围

check out this SO answer 看看这个答案

I don't think it is the condition switcher % 2 == 0 that is problematic here. 我不认为这是问题switcher % 2 == 0 You have hooked events in case of true but is there an Else statement that unhooks those events so that the original functionality is resumed? 您已经钩住了事件,如果情况为true,但是是否存在其他语句来取消钩住这些事件,以便恢复原始功能? ie Right click create default context menu. 即,右键单击创建默认上下文菜单。

Update: 更新:

In order to resume original functionality, call 为了恢复原始功能,请致电

document.oncontextmenu = null;

in else part. 在其他部分。

Also, you need to define $('#page:not(#newid)').mousedown(function (e) { only once (outside of if/else ) and then use the switcher variable to determine whether to call the functionality or not. 另外,您需要定义$('#page:not(#newid)').mousedown(function (e) {仅一次(在if / else之外),然后使用switcher变量确定是否调用该功能或不。

In short, you need the following 简而言之,您需要以下内容

$(document).ready(function () {
    /* Set switcher to zero */
    switcher = 0;
    /* If switch gets clicked increment var switcher*/
    $('#guidenavschalter').click(function () {
        switcher++;
        return false;
    });

    document.oncontextmenu = function() {
        if ( switcher % 2 == 0 ) {
            return false;
        } else {
            return true;
        }
    };
    /* if click within #page excluding area of #newid */
    $('#page:not(#newid)').mousedown(function (e) {
        if (switcher % 2 == 0) {
            // do stuff
        } else {
            // do nothing
        }
    });

});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM