【问题标题】:Is this the correct way to use localStorage?这是使用 localStorage 的正确方法吗?
【发布时间】:2021-04-29 18:49:31
【问题描述】:

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

$(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"));
        }
    });
});

这是我的模板:

<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 的使用方式的处理方式有关,但我不确定如何处理。

【问题讨论】:

  • 我没有看到你在$data()中设置了theme
  • @Rojo 你的意思是var theme = $("#theme");
  • @Barmar 不,我的意思是$(this).data("theme")
  • 来自 HTML 中的data-theme="..."
  • 我想你想要if (getTheme == $(this).val())

标签: javascript html css local-storage


【解决方案1】:

你的代码[对我来说]看起来很复杂

只是做

<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>
const theSelect = document.querySelector('#select')

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

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

【讨论】:

    【解决方案2】:

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

    $(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);
    
    });
    

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

    $(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);
    
    });

    【讨论】:

    • 谢谢!有没有办法让页面转换看起来不那么“笨拙”?当我换页时,可以短暂看到原始主题,然后选择的主题被选中。
    • 另外,还有“select[0].value = themeHref”的替代品吗?我收到“jquery-3.3.1.slim.min.js:2 Uncaught TypeError: Cannot set property 'value' of undefined”
    • 那么为什么选择元素不在页面上呢?这就是该错误的原因
    • 你怎么能减少它......你可以document.write出样式表
    • 不确定我是否完全理解您的问题?为什么选择元素不在页面上?它是。它可能是我引用 .js 代码的地方。我将它放置在一个布局模板中,其他所有页面都参考它的一般样式。
    猜你喜欢
    • 2012-05-11
    • 2018-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-05
    相关资源
    最近更新 更多