[英]Show div if cookie is set
I'm using js-cookie to set a cookie. 我正在使用js-cookie设置cookie。 I've got a div which is set to display: none;
我有一个div设置为display: none;
and if the cookie is set I want to change it to display:block;
如果设置了cookie,我想将其更改为display:block;
. 。
The HTML: HTML:
<div id="cookie-hide">
<p>test</p>
</div>
The CSS: CSS:
<style>
#cookie-hide {display: none;}
</style>
This is the JS: 这是JS:
<script>
function checkCookie() {
var user= Cookies.get('video')
if (user != "") {
document.getElementById("cookie-hide").style.display = "block";
}
}
Cookies.set('video', 'play');
</script>
I can see in chrome that the cookie has been set but the div still doesn't display. 我可以在Chrome浏览器中看到已设置了cookie,但div仍然没有显示。 Any ideas what's wrong with the code? 任何想法代码有什么问题吗?
Do it as: 这样做:
if (user == "") {
var x = document.getElementById("cookie-hide");
x.style.display = "block";
}
This should work 这应该工作
The code is fine. 代码很好。 You are not calling the function anywhere. 您没有在任何地方调用该函数。
function checkCookie() {
var user= Cookies.get('video')
if (user != "") {
document.getElementById("cookie-hide").style.display = "block";
}
}
Cookies.set('video', 'play');
checkCookie(); // <----- This line.
DEMO: https://jsfiddle.net/u1x030b4/ 演示: https : //jsfiddle.net/u1x030b4/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.