首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >这是使用localStorage的正确方式吗?

这是使用localStorage的正确方式吗?
EN

Stack Overflow用户
提问于 2021-01-26 07:11:12
回答 2查看 58关注 0票数 0

我希望能够使用元素来切换主题。主题存储在不同的.css文件中,通过选择不同的<option>,我可以选择不同的主题。然而,切换页面会将主题切换回我当前写入到实际模板中的内容。这是我的.js文件:

代码语言:javascript
运行
复制
$(function() {
    var theme = $("#theme");
    var getTheme = localStorage.getItem("theme");
    
    
    $("#select option:selected").each(function() { /* Switch on-change function */
        if (getTheme == $(this).data("theme")) {
            theme.attr("href", getTheme); /* Set the theme of the page to whatever theme applies to this option */
            console.log($(this).data("theme"));
        }
        else {
            theme.attr("href", $(this).data("theme"));
            localStorage.setItem("theme", $(this).data("theme")); /* Store the choice into local storage; should persist upon page reload */
            console.log($(this).data("theme"));
        }
    });
});

下面是我的模板:

代码语言:javascript
运行
复制
<div style="text-align: center">
  <select class="form-control" name="select" id="select">
      <option selected value="original" data-theme="/static/styles.css">Original</option>
      <option value="darkmode" data-theme="/static/stylesdark.css">Dark Mode</option>
      <option value="cybermode" data-theme="/static/stylescyber.css">Cyber Mode</option>
      <option value="fire" data-theme="/static/stylesfire.css">Fire & Brimstone</option>
  </select>
</div>

我认为我的错误与我使用localStorage的处理方式有关,但我不确定是如何处理的。

EN

回答 2

Stack Overflow用户

发布于 2021-01-26 07:26:05

你的代码对我来说似乎很复杂

只管去做

代码语言:javascript
运行
复制
<div style="text-align: center">
  <select class="form-control" name="select" id="select">
    <option value="/static/styles.css"     > Original      </option>
    <option value="/static/stylesdark.css" > Dark Mode      </option>
    <option value="/static/stylescyber.css"> Cyber Mode      </option>
    <option value="/static/stylesfire.css" > Fire & Brimstone </option>
  </select>
</div>
代码语言:javascript
运行
复制
const theSelect = document.querySelector('#select')

theSelect.value = localStorage.getItem('theme') || theSelect.options[0].value 

theSelect.onchange = e =>
  {
  localStorage.setItem('theme', theSelect.value)
  }
票数 0
EN

Stack Overflow用户

发布于 2021-01-26 07:34:56

你的代码比它需要的复杂得多。

代码语言:javascript
运行
复制
$(function() {

  var theme = $("#theme");
  var select = $("#select");

  function setStyleSheet(selectOption) {
    // use local storage, if no value, use default
    var themeHref = localStorage.getItem("theme") || "/your/default.css";
    // update the stylesheet
    theme.attr("href", themeHref);
    // if we need to, update the option with the value from local storage
    if (selectOption) {
      select.find('option[data-value="' + themeHref + '"]').prop('selected', true);
    }
  }

  // bind change event to the select
  select.on('change', function() {
    // grab selected option
    var theme = select.find("option:selected").data("theme");
    // update local storage
    localStorage.setItem("theme", theme);
    // update the style sheeet
    setStyleSheet(false);
  });

  // update the stylesheet on page load
  setStyleSheet(true);

});

如果使用值而不是数据属性,这将更加简单。

代码语言:javascript
运行
复制
$(function() {

  var theme = $("#theme");
  var select = $("#select");

  function setStyleSheet(selectOption) {
    // use local storage, if no value, use default
    var themeHref = localStorage.getItem("theme") || "/your/default.css";
    // update the stylesheet
    theme.attr("href", themeHref);
    // if we need to, update the option with the value from local storage
    if (selectOption) {
      select[0].value = themeHref;
    }
  }

  // bind change event to the select
  select.on('change', function() {
    // grab selected option's value
    var theme = this.value;
    // update local storage
    localStorage.setItem("theme", theme);
    // update the style sheeet
    setStyleSheet(false);
  });

  // update the stylesheet on page load
  setStyleSheet(true);

});

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65893679

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档