亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

還記得 jQuery 彈出模式的關閉狀態嗎,localStorage 是最簡單的方法嗎?

還記得 jQuery 彈出模式的關閉狀態嗎,localStorage 是最簡單的方法嗎?

慕無忌1623718 2023-07-06 16:41:33
我試圖使用 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>
查看完整描述

2 回答

?
qq_花開花謝_0

TA貢獻1835條經驗 獲得超7個贊

很好的問題!如果我是你,我會更多地研究 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">

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

</div>


<script>

    //Get current localStorage value

    const gotoInternationalValue = localStorage.getItem('gotoInternational');

    //Check if it is not true (can be false or null)

    if (gotoInternationalValue != 'true') {

        setTimeout(function() {

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

        }, 3000)

    } else {

        console.log("Stay at international was already selected");

    }



    function gotoInternational() {

        //Save localStorage item as true

        localStorage.setItem('gotoInternational', true);

    }

</script>

如果這不是您所需要的,請在您的問題中添加更多信息以澄清它!


查看完整回答
反對 回復 2023-07-06
?
波斯汪

TA貢獻1811條經驗 獲得超4個贊

我找到了解決方案!

您的解決方案是正確的,只是您在這里錯過了一個引號:
if (gotoInternationalValue != 'true' )

無論如何,感謝您的投入!拯救了我的一天!


查看完整回答
反對 回復 2023-07-06
  • 2 回答
  • 0 關注
  • 158 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號