2013-03-16 61 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