设置元素的显示并设置cookie以使其在点击时隐藏

时间:2011-10-24 17:23:37

标签: javascript cookies

我正在寻找一种简单的方法来隐藏元素并设置一个cookie,以便在点击时隐藏它。

<div id="hide-me">I need to be hidden</div>

<div id="hiding">
 If I get clicked, #hide-me's display style goes from block to none.
 Now the next time this person visits again, #hide-me will be display:none.
</div>

1 个答案:

答案 0 :(得分:0)

Example

var COOKIE_NAME = "hideMeDisplay",
    hideMe = document.getElementById('hide-me'),
    cookie = getCookie(COOKIE_NAME),
    hideMeDisplay = cookie  ? cookie : hideMe.style.display,
    hiding = document.getElementById('hiding');

hiding.onclick = function() {
    setCookie(COOKIE_NAME, hideMeDisplay, 100);
    hideMe.style.display = hideMeDisplay === "block" ? "none" : "block";
    hideMeDisplay = hideMe.style.display;

}
hiding.onclick(); // trigger first click to set cookie