我正在寻找一种隐藏元素并设置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>

最佳答案

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

关于javascript - 设置元素的显示并设置cookie以使其在单击时保持隐藏,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/7879346/

10-11 14:39