在jquery中为hide / show div块设置cookie

时间:2014-03-22 21:17:28

标签: jquery cookies

单击“关闭阻止”后,刷新页面时不会保存操作。我究竟做错了什么?

请提示, 提前谢谢。

$(window).load(function(){

// setup the initial display on page load
var menu_state = $.cookie('welcome');

    if( typeof menu_state !== "undefined" && menu_state == "hidden" ) {
        $('#welcome_block').hide; // visible, and cookie is set
        $.cookie('welcome', 'hidden'); // set the cookie
        console.log( 'hide' );
        console.log( $.cookie('welcome') );
    } else {
        $('#welcome_block').show; // hidden or cookie is not set, set just in case
        $.removeCookie('welcome'); // prevent multiple instances
        console.log( 'show' );
    }

    console.log( $.cookie('welcome') );

    $('.disabled').click(function() {
        console.log( 'disable block' );

        $('#welcome_block').toggle();
        $.removeCookie('welcome'); // prevent multiple instances
        $.cookie('welcome', 'hidden'); // update (or set) the cookie
        $(".disabled").css("display", "none");
        $(".enabled").css("display", "block");
        //console.log( $.cookie('welcome') );
    });

    $('.enabled').click(function() {
        console.log( 'enable block' );

        $('#welcome_block').toggle();
        $.removeCookie('welcome'); // prevent multiple instances
        $.cookie('welcome', 'visible'); // update (or set) the cookie
        $(".disabled").css("display", "block");
        $(".enabled").css("display", "none");
        //console.log( $.cookie('welcome') );
    });

});

更多:http://jsfiddle.net/narzankmv/bwLSu/4/

1 个答案:

答案 0 :(得分:0)

  1. 您使用方法"隐藏" &安培; "显示"没有()
  2. 此方法使用css prop。 "显示",而不是"可见性"
  3. 这是JS代码的工作示例HTML:

    $(window).load(function(){
    // setup the initial display on page load
    var menu_state = $.cookie('welcome');
    
    if( (typeof menu_state !== "undefined") && (menu_state == "hidden")) {
        $('#welcome_block').css("visibility", "hidden");
        $(".enabled").css("display", "block");
        // visible, and cookie is set
    } else {
        $(".enabled").css("display", "none");
        $('#welcome_block').css("visibility", "visible") 
        // hidden or cookie is not set, set just in case
    }
    
    $('.disabled').click(function() {        
        $('#welcome_block').css("visibility", "hidden");
        $.removeCookie('welcome'); // prevent multiple instances
        $.cookie('welcome', 'hidden'); // update (or set) the cookie
        $(".disabled").css("display", "none");
        $(".enabled").css("display", "block");
        //console.log( $.cookie('welcome') );
    });
    
    $('.enabled').click(function() {
        $('#welcome_block').css("visibility", "visible");
        $.removeCookie('welcome'); // prevent multiple instances
        $.cookie('welcome', 'visible'); // update (or set) the cookie
        $(".disabled").css("display", "block");
        $(".enabled").css("display", "none");
        //console.log( $.cookie('welcome') );
    }); });