单击包装器时隐藏div

时间:2014-01-25 22:58:10

标签: javascript jquery css

var x = 0;
if (x == 0) {
    $('#tap').click(function() {
        $('#menuUser').show();
    });
    x = 1;
    document.getElementById('var').innerHTML = "" + x + "";
}
else if (x == 1) {
    $('.#wpId').click(function() {
        $('#menuUser').hide();
    });
    x = 0;
}

我使用它来隐藏#menuUser x=1,但它不起作用。

1 个答案:

答案 0 :(得分:0)

您需要在DOM ready上附加事件处理程序,如下所示:

$(function () {
    var x = 0;

    $('#wpId').click(function () {
        $('#menuUser').hide();
        //if x is needed for something else
        x = 0;
        $('#var').text(x);
    });


    $('#tap').click(function (ev) {
        ev.stopPropagation();
        $('#menuUser').show();
        //if x is needed for something else
        x = 1;
        $('#var').text(x);
    });
});

你可以在这里测试一下:

http://jsfiddle.net/csicky/A7p37/1/