使用jquery cookie保存div切换状态

Mat*_*att 2 jquery jquery-cookie

我知道如何实现这一点,但似乎并非所有相似的帖子,任何人都可以给出一个简单的例子.我想简单地将"marketing-message-global"的切换状态值存储在cookie中.如果用户单击"hide-marketing-message-btn",则切换状态将存储在cookie中.当用户刷新页面时,将使用存储的切换状态,并隐藏已切换的div.

<div id="marketing-message-global">
</div>
<div id="hide-marketing-message-btn">
</div>

$(document).ready(function() {
 if $('#hide-marketing-message-btn").clicked()
 {
    $("#marketing-message-global").hide();
    $.cookie("toggle-state") == true;
 }

if ($.cookie("toggle-state") == true)
{
    $("#marketing-message-global").hide();
}
else if ($.cookie("toggle-state") == false)
{
$("#marketing-message-global").show();
}
 });
</script>
Run Code Online (Sandbox Code Playgroud)

Sph*_*inx 5

我使用了jquery cookie插件(https://github.com/carhartl/jquery-cookie)

    $(function(){
       if($.cookie){
           //By default, if no cookie, just display.
           $("#marketing-message-global").toggle(!(!!$.cookie("toggle-state")) || $.cookie("toggle-state") === 'true');
    }

    $('#hide-marketing-message-btn').on('click', function(){
        $("#marketing-message-global").toggle();
        //Save the value to the cookie for 1 day; and cookie domain is whole site, if ignore "path", it will save this cookie for current page only;
        $.cookie("toggle-state", $("#marketing-message-global").is(':visible'), {expires: 1, path:'/'}); 
});

});
Run Code Online (Sandbox Code Playgroud)