Jquery使用cookie来显示和隐藏DIV

时间:2014-01-28 09:49:50

标签: jquery

您好我需要hellp来设置它。请!

这是我的jquery代码:

$(document).ready(function() {
            var shouldShow = $.cookie('show_languages') == 'yep';
            if(shouldShow) {
                $('.popupLang').show();
            } else {
                $('.popupLang').hide();
            }

            $('.popupLang').show();
            $.cookie('show_languages', 'yep');
            return false;

            $('#choose').click(function() {
                    $('.popupLang').hide();
                    $.cookie('show_languages', 'nope');
                    return false;
            });
});

我想要的是什么:

当页面加载时,我有一个弹出窗口的div容器并覆盖页面的其余部分,当id为“选择 popupLang ” >“点击容器” popupLang “必须隐藏,并且Cookie必须记住这一点,这样当再次打开此页面时,DIV” popupLang “必须保持隐藏状态。

为了做到这一点,我必须在代码中添加什么?

1 个答案:

答案 0 :(得分:1)

应该是

$(document).ready(function () {
    var shouldShow = $.cookie('show_languages') != 'nope';
    $('.popupLang').toggle(shouldShow);

    $('#choose').click(function () {
        $('.popupLang').hide();
        $.cookie('show_languages', 'nope');
        return false;
    });
});

您的代码

$(document).ready(function () {
    //first time when the page loads the cookie value is undefined so the condition will fail
    var shouldShow = $.cookie('show_languages') == 'yep';
    if (shouldShow) {
        $('.popupLang').show();
    } else {
        $('.popupLang').hide();
    }

    //irrespective of the cookie value you are setting the visibility to true
    $('.popupLang').show();
    $.cookie('show_languages', 'yep');
    //you re returning here that means the click handler is never registered
    return false;

    //this code never gets executed
    $('#choose').click(function () {
        $('.popupLang').hide();
        $.cookie('show_languages', 'nope');
        return false;
    });
});