我的网站上有这个文字横幅。我希望它只向使用cookie的新访客显示。我对JS知之甚少,所以请帮助我如何才能向首次访问者展示它。
我在基于话语的this Android forum site上使用它,它已经构建在Node.js上。
<style>
.top {
background: linear-gradient(to right, #141517, #6A9113);
border-radius: 10px;
font-size: 20px;
color: white;
opacity: 0.9;
text-align: center;
padding: 25px;
line-height: 30px;
margin: 10px 0px 30px 0px;
}
</style>
<div class="top">
Welcome to the forum. This is demo text.
</div>
答案 0 :(得分:0)
转到此代码:
.top
默认为display: none;
$(document).ready(function() {
var visited = $.cookie("visited")
if (visited == null) {
$('.top').show();
alert($.cookie("visited"));
}
$.cookie('visited', 'yes', { expires: 1, path: '/' });
});
答案 1 :(得分:0)
这是完整的代码...... 工作演示https://jsfiddle.net/hdas2012/6yjo346k/
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-cookie/1.4.1/jquery.cookie.min.js"></script>
<div class="top">
<h2>You will only see it once</h2>
Welcome to the forum. This is demo text.
</div>
<script>
$(document).ready(function(){
if(!$.cookie('welcomeMsg')){
$(".top").show();
$.cookie('welcomeMsg', 'Y', { expires: 365*3 });
}
});
</script>
<style>
.top {
background: linear-gradient(to right, #141517, #6A9113);
border-radius: 10px;
font-size: 20px;
color: white;
opacity: 0.9;
text-align: center;
padding: 25px;
line-height: 30px;
margin: 10px 0px 30px 0px;
display: none;
}
</style>