为什么刷新页面时本地存储失败?

时间:2021-07-08 22:15:16

标签: javascript local-storage

每当我在我的应用程序中切换到夜间模式并刷新我的页面或转到个人资料时,夜间模式就会关闭。出了什么问题,我该如何解决?

(function(window, document, undefined) {
  'use strict';
  if (!('localStorage' in window)) return;
  var nightMode = localStorage.getItem('gmtNightMode');
  if (nightMode) {
    document.documentElement.className += ' night-mode';
  }
})(window, document);

(function(window, document, undefined) {

  'use strict';

  // Feature test
  if (!('localStorage' in window)) return;

  // Get our newly insert toggle
  var nightMode = document.querySelector('#night-mode');
  if (!nightMode) return;

  // When clicked, toggle night mode on or off
  nightMode.addEventListener('click', function(event) {
    event.preventDefault();
    document.documentElement.classList.toggle('dark');
    if (document.documentElement.classList.contains('dark')) {
      localStorage.setItem('gmtNightMode', true);
      return;
    }
    localStorage.removeItem('gmtNightMode');
  }, false);

})(window, document);

0 个答案:

没有答案