我需要有关 cookie 消息弹出窗口的帮助。我希望该消息只显示一次每个用户点击接受。 这是代码:https://jsfiddle.net/bz8ur3wu/1/
$(document).ready(function(){
$("button").click(function(){
$(".cookie").fadeOut();
});
});
.cookie{width:100%;padding:20px;background:rgb(231, 231, 231);bottom:0;left:0;position:fixed;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="cookie">
This website use cookies.
<button>accept</button>
</div>
最佳答案
作为替代解决方案,我建议使用 localstorage
。查找 cookieconsent
值,如果找到,则将类添加到 body
;否则在用户单击按钮时设置值。
$(document).ready(function(){
if (!!localStorage.getItem("cookieconsent")) {
document.body.classList.add("cookieconsent")
}
else {
$("button").click(function() {
localStorage.setItem("cookieconsent", "ok")
$(".cookie").fadeOut();
});
}
});
然后在您的 CSS 中仅当用户之前未接受时才显示 .cookie 元素,也就是说,如果 body
没有 .cookieconsent
类
.cookie {
display: none; }
body:not(.cookieconsent) .cookie {
display: block;
}
关于javascript - 每次点击/提交按钮接受时,Cookie 消息仅显示一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50526865/