簡體   English   中英

如何根據本地存儲中的數據切換主題

[英]How do I switch my theme based on data from local storage

盡管從本地存儲中讀取主題設置,但我的主題設置始終會恢復為頁面刷新時的默認主題

這是我的主題實現代碼:

green.addEventListener('click', () => {
  if (localStorage.getItem('theme') != 'green') {
    localStorage.setItem('theme', 'green');
  }
  main.classList.add(localStorage.getItem('theme'));
});
yellow.addEventListener('click', () => {
  if (localStorage.getItem('theme') !== 'yellow') {
    localStorage.setItem('theme', 'yellow');
  }
  main.className = localStorage.getItem('theme');
});
purple.addEventListener('click', () => {
  if (localStorage.getItem('theme') !== '') {
    localStorage.setItem('theme', '');
  }
  main.className = localStorage.getItem('theme');
});

我檢查了localStorage並存儲了主題值

在Initial類中,不要將其設置為空,將本地存儲設置為默認主題,並從初始類的本地存儲中讀取主題值。 因此,在頁面重新加載時,您將從本地存儲中讀取主題,並且將使用自定義主題進行設置。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM