还记得 jQuery 弹出模式的关闭状态吗,localStorage 是最简单的方法吗?

我试图使用 localStorage 来记住用户单击“留在国际”时的选择。这样,每当用户访问该页面时,弹出窗口就不会一直提示。


我找不到一种方法来记住 jQuery 模式的关闭状态并将其放入 localStorage cookie 中。我们将非常感谢您的帮助!


<!-- jQuery Modal -->

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-modal/0.9.1/jquery.modal.min.js"></script>

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jquery-modal/0.9.1/jquery.modal.min.css" />


<div id="countryModal" NAME="country" class="modal">

<a rel="modal:close" href="#" onclick="gotoInternational()">Stay at International</a>

</div>


<script>

setTimeout(function(){

  jQuery('#countryModal').fadeIn('slow').modal('show');

},3000)


function gotoInternational(){

//localStorage codes here

}

</script>


慕无忌1623718
浏览 113回答 2
2回答

qq_花开花谢_0

很好的问题!如果我是你,我会更多地研究 localStorage。您完全正确地将值存储在 localStorage 中是最好的方法。如果我正确理解你想要做什么,你的代码应该如下所示:<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script><!-- jQuery Modal --><script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-modal/0.9.1/jquery.modal.min.js"></script><link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jquery-modal/0.9.1/jquery.modal.min.css" /><div id="countryModal" NAME="country" class="modal">&nbsp; &nbsp; <a rel="modal:close" href="#" onclick="gotoInternational()">Stay at International</a></div><script>&nbsp; &nbsp; //Get current localStorage value&nbsp; &nbsp; const gotoInternationalValue = localStorage.getItem('gotoInternational');&nbsp; &nbsp; //Check if it is not true (can be false or null)&nbsp; &nbsp; if (gotoInternationalValue != 'true') {&nbsp; &nbsp; &nbsp; &nbsp; setTimeout(function() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; jQuery('#countryModal').fadeIn('slow').modal('show');&nbsp; &nbsp; &nbsp; &nbsp; }, 3000)&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; console.log("Stay at international was already selected");&nbsp; &nbsp; }&nbsp; &nbsp; function gotoInternational() {&nbsp; &nbsp; &nbsp; &nbsp; //Save localStorage item as true&nbsp; &nbsp; &nbsp; &nbsp; localStorage.setItem('gotoInternational', true);&nbsp; &nbsp; }</script>如果这不是您所需要的,请在您的问题中添加更多信息以澄清它!

波斯汪

我找到了解决方案!您的解决方案是正确的,只是您在这里错过了一个引号:if (gotoInternationalValue !=&nbsp;'true'&nbsp;)无论如何,感谢您的投入!拯救了我的一天!
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript