我正在寻找一种简单的方法来隐藏元素并设置一个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>
答案 0 :(得分:0)
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