我使用以下代碼來切換特定的 div 并使用 cookie 保存設置,以便它保留更改。但由于某種原因,保存 cookie 部分無法正常工作。例如,使用以前的瀏覽器按鈕時,似乎沒有保存 cookie。我在當前代碼中缺少什么完美添加 cookie 并檢查它。function getCookieValue(a) { var b = document.cookie.match('(^|;)\\s*' + a + '\\s*=\\s*([^;]+)'); return b ? b.pop() : '';}$(document).ready(function() { var redEl = $('input#togglebtw'); if (document.cookie.indexOf('togglebtw=') != -1) { redEl.prop("checked", $.parseJSON(getCookieValue("togglebtw"))); } if (redEl.prop("checked")) { $(".price-container .price-including-tax").hide(); $(".price-container .price-excluding-tax").show(); } else { $(".price-container .price-excluding-tax").hide(); $(".price-container .price-including-tax").show(); } $('input#togglebtw').click(function() { var expiryDate = new Date(); expiryDate.setDate(expiryDate.getDate() + 31); expiryDate = expiryDate.toUTCString(); if ($(this).attr("class") == "btwToggler") { if (redEl.prop("checked")) { $(".price-container .price-including-tax").hide(); $(".price-container .price-excluding-tax").show(); } else { $(".price-container .price-excluding-tax").hide(); $(".price-container .price-including-tax").show(); } document.cookie = "togglebtw=" + this.checked.toString() + "; expires=" + expiryDate; } });});<input id="togglebtw" class="btwToggler" type="checkbox">
切換 div 并設置 cookie 以保存 jQuery
皈依舞
2022-06-05 17:00:14