2016-04-28 69 views
-1

我的网站上有一个粘性横幅,鼓励用户注册一个简报。但它可能很烦人,如果它不断出现在页面上,可能会挫败用户。使用jQuery存储用户选择localStorage

所以,我为横幅添加了一个十字,将其关闭。我想要做的是一旦它被选中为关闭状态,将其存储为某种形式的本地存储,检查并根据用户是否已关闭显示/隐藏横幅。

这是可能的,如果是这样,我该怎么去做。

谢谢

+0

是的,你可以这样做。但我会建议你只是使用cookies来做到这一点。 – Kiksen

回答

2

是的,这是可能的,你正在思考正确的方式。

这里是DEMO

假设HTML和JS


HTML

<div class="sticky"> 
Do not show on close 
</div> 
<button class="close"> 
Close sticky 
</button> 

JS

$(document).ready(function(){ 
    var localStor=localStorage.getItem("stickyClosed"); //get the localstorage value 
    if(localStor=="true") //check if its true 
     $('.sticky').hide(); //hide or remove the sticky element 
}) 

$('.close').on('click',function(){ 
    $('.sticky').remove(); //remove on click of close 
    localStorage.setItem('stickyClosed','true') //set localstorage value 
}) 
+0

辉煌,谢谢! – StuBlackett

+0

随时随地..快乐编码.. :) –