Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/421.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何记住使用cookie显示和隐藏div_Javascript_Jquery_Html_Css_Cookies - Fatal编程技术网

Javascript 如何记住使用cookie显示和隐藏div

Javascript 如何记住使用cookie显示和隐藏div,javascript,jquery,html,css,cookies,Javascript,Jquery,Html,Css,Cookies,我的HTML格式如下: <div id='mainleft-content'>content is visible</div> <div id="expand-hidden">Button Expand +</div> 我想使用cookie来记住div的状态是隐藏或显示访问者的操作 我怎么做?谢谢你的帮助 请参见 您可以使用is(“:visible”)为此,它将返回div是否可见: if ( $("#mainleft-content").is("

我的HTML格式如下:

<div id='mainleft-content'>content is visible</div>
<div id="expand-hidden">Button Expand +</div>
我想使用cookie来记住div的状态是隐藏或显示访问者的操作

我怎么做?谢谢你的帮助

请参见

您可以使用
is(“:visible”)
为此,它将返回div是否可见:

if ( $("#mainleft-content").is(":visible") ){
   alert('its visible');
}
else{
   alert('div is hidden');
}
如果您仍然需要cookies,您可以添加一个函数:

function setCookie(c_name, value, exdays) {
    var exdate = new Date();
    exdate.setDate(exdate.getDate() + exdays);
    var c_value = escape(value) + ((exdays == null) ? "" : "; expires=" + exdate.toUTCString());
    document.cookie = c_name + "=" + c_value;
}

function getCookie(c_name) {
    var i, x, y, ARRcookies = document.cookie.split(";");
    for (i = 0; i < ARRcookies.length; i++) {
        x = ARRcookies[i].substr(0, ARRcookies[i].indexOf("="));
        y = ARRcookies[i].substr(ARRcookies[i].indexOf("=") + 1);
        x = x.replace(/^\s+|\s+$/g, "");
        if (x == c_name) {
            return unescape(y);
        }
    }
}
通过jQuery使用:

您可以使用插件:

然后把状态设置为

$.cookie("visible", 1);
使用


你还没有提到我的问题。cookie呢?你能告诉我演示的最新情况吗?我进行了测试,但无法保存状态。我怎么了(@happi check fiddle now谢谢你的建议。
$(document).ready(function () {
    $("#expand-hidden").click(function () {
        $("#mainleft-content").toggle();
        SetCookie("DivStateVisible", $("#mainleft-content").is(":visible"),5);
    });
});
function setCookie(c_name, value, exdays) {
    $.cookie(c_name, value, { expires : exdays });
}

function getCookie(c_name) {
    return $.cookie(c_name);
}
$.cookie("visible", 1);
$(document).ready(function () {
    $("#mainleft-content").toggle(!!$.cookie("visible"));
    $("#expand-hidden").click(function () {
        $("#mainleft-content").toggle(function() { 
             $.cookie("visible", $("#mainleft-content").is(':visible') ? 1 : 0);
        });
    });
});