【问题标题】:Save change to background using localstorage使用本地存储将更改保存到后台
【发布时间】:2014-11-02 16:58:53
【问题描述】:

我正在尝试存储用户单击基于选择列表所做的最新样式选择。我曾尝试使用 localstorage 来执行此操作,但我很难让它工作。

任何帮助将不胜感激。谢谢!

这是我在看了几个例子后能想到的最好的方法 - 但它不起作用。

function initiate(){
    var styleButton = document.getElementById("selectStyleButton");
    if (styleButton){
        styleButton.addEventListener("click", saveStyle);
    }
    setStyle();
}

function saveStyle(){
    var select = document.getElementById("selectStyle");
    if (select){
        select[select.selectedIndex].value = localStorage.setItem("selectStyle"); //store value that was selected
    }
    setStyle(); //set the style that was selected based on the above
}

function setStyle(){
    var newstyle = localStorage.getItem("selectStyle"); //get value that was selected
    document.body.className = newstyle; //change body based on the class name of value that was selected
    }

window.addEventListener("load", initiate);

【问题讨论】:

    标签: javascript html local-storage


    【解决方案1】:

    您在这里尝试做的是设置所选选项的值,而不是将其存储在本地存储中。试试吧

     localStorage.setItem("selectStyle",select[select.selectedIndex].value);
    

    http://diveintohtml5.info/storage.html#methods

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-09-22
      • 2018-11-05
      • 2016-04-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多