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);
哇,谢谢它的作品!终于!这花了我整整一天 – MmDott 2013-03-16 03:09:59