2013-03-16 78 views
1

我试图通过基于选择列表的用户单击来存储最近的样式选择。我试图用localstorage来做这件事,但我很难让它工作。使用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); 

回答

0

您试图在此处执行的操作是将所选选项的值设置为不将其存储在本地存储中。尝试代替

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

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

+0

哇,谢谢它的作品!终于!这花了我整整一天 – MmDott 2013-03-16 03:09:59