[英]how to store a function so it works only after clicked and results stays after refreshing the page?
function hd() { document.getElementById("Y").style.display = "none"; }
body { display:grid; place-content: center; height:100vh; overflow:hidden; }
<div id="Y">i will be hidden</div> <br> <button onclick="hd();">click me</button>
The important thing is to store the function so it works even user refreshes the page I tried to use cookies but it did not work by just calling the function重要的是存储 function 所以即使用户刷新我尝试使用 cookies 的页面它也可以工作,但仅通过调用 function 就无法工作
You could set a flag with localStorage
and then check whether this flag has been set already and call the function at startup again.您可以使用localStorage
设置一个标志,然后检查该标志是否已经设置,并在启动时再次调用 function。
function hd () {
document.getElementById("Y").style.display = "none";
localStorage.setItem("hidden", "yes");
}
if (localStorage.getItem("hidden")) {
hd();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.