-1

我坐在下面的代码片段前面几个小时,无法让它像我想要的那样运行。

基本上,这段代码在右键单击时会创建一个导航菜单,但是单击切换器应该关闭此功能,并且在下一次单击时它会再次打开。

一切正常(如预期),只是第 12 行的小 if 语句( if ( switcher % 2 == 0 ) )没有按预期工作,这意味着无论 var switcher 是否偶数,其中的代码总是会执行。我还尝试了其他条件,例如“> 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;
        });
    }
});
4

3 回答 3

3

你的代码基本上是

switcher = 0;

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

if ( switcher % 2 == 0 ) {

所以难怪测试总是通过。

您可能想要的是将if回调放在内部,以便每次单击时都会对其进行测试:

var switcher = 0;
$('#guidenavschalter').click(function(){
    switcher++;
    if ( switcher % 2 == 0 ) {
       ...
    }
    return false;
});
于 2013-07-23T10:29:26.950 回答
1
switcher = 0;  // created outside the click event handler

并且您正在增加 click 事件处理程序中的值。因此它始终为零。

您应该通过JavaScript 中的范围界定

从评论中,您有兴趣了解更多关于Javascript中的变量范围的信息

看看这个答案

于 2013-07-23T10:29:22.410 回答
0

我不认为这是switcher % 2 == 0有问题的条件。在 true 的情况下,您已经挂钩了事件,但是是否有一个 Else 语句可以取消挂钩这些事件以便恢复原始功能?即右键单击创建默认上下文菜单。

更新:

为了恢复原始功能,请致电

document.oncontextmenu = null;

在其他部分。

此外,您只需定义$('#page:not(#newid)').mousedown(function (e) {一次(在 if/else 之外),然后使用 switcher 变量来确定是否调用该功能。

简而言之,您需要以下内容

$(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
        }
    });

});
于 2013-07-23T10:29:51.837 回答