从本地存储存储和加载下拉选择
Store and Load dropdown selection from localStorage
我正在尝试将选项值保存到localstorage,以便在打开其他页面或返回网站时保存选项并使用与上次打开网站时相同的css文件。
这是我到目前为止所做的,但我无法让它工作:
.HTML:
<select name="style" id="style" onChange="changeCSS();">
<option id="standard" value="standard">Standard</option>
<option id="alternative" value="alternative">Alternative</option>
</select>
Javascript:
function changeCSS() {
"use strict";
var select, stylesheet, save;
select = document.getElementById("style");
stylesheet = document.getElementById("stylesheet");
if(localStorage.getItem('save')) {
select.options[localStorage.getItem('save')].selected = true;
}
if (select.value === "standard") {
stylesheet.href = "include/global.css";
localStorage.setItem('save', select.value);
} else if (select.value === "alternative") {
stylesheet.href = "include/alternative.css";
localStorage.setItem('save', select.value);
}
}
最终设法让它工作。这是我所做的:
.HTML:
<select name="style" id="style" onChange="changeCSS();">
<option id="standard" value="standard">Standard</option>
<option id="alternative" value="alternative">Alternative</option>
</select>
将此添加到正文标签:
<body onload="autoCSS();">
Javascript:
var select, stylesheet;
function changeCSS() {
"use strict";
select = document.getElementById("style");
stylesheet = document.getElementById("stylesheet");
if (select.value === "standard") {
stylesheet.href = "include/global.css";
localStorage.setItem('save', select.value);
} else if (select.value === "alternative") {
stylesheet.href = "include/alternative.css";
localStorage.setItem('save', select.value);
}
}
function autoCSS() {
"use strict";
select = document.getElementById("style");
stylesheet = document.getElementById("stylesheet");
if (localStorage.getItem('save')) {
select.options[localStorage.getItem('save')].selected = true;
changeCSS();
}
}
相关文章:
- 选择文本并添加到本地存储
- 从Function触发JQuery UI日期选择器,并将日期存储到变量中
- 将用户选择的数据临时存储在我的阵列中
- 使用 jquery .find() 遍历按类和存储属性选择元素
- Javascript 在数组中存储<选择>下拉值
- 表单选择不保存在本地存储中
- 单击提交时存储输入单选选择
- 如何刷新存储和快照的 jquery 选择器变量
- 无法检索从窗口.本地存储中选择输入值
- 性能问题:存储对DOM元素的引用与使用选择器相比
- 在使用java使用dropbox选择器从dropbox中提取文件后,如何将文件存储在服务器中
- 将日期值(存储在变量中)分配给日期选择器
- 如何使用存储的属性来选择下拉值
- html5文件api,将用户选择的目录存储在sessionStorage中
- 跨多个页面存储选择/下拉值
- 存储值后,重复重新选择选择下拉菜单的第一个选项
- jQuery data():can't使用jQuery选择器获取存储的数据
- 在数组中存储 JQuery 字段与仅存储选择字符串相比如何
- 存储选择框的选项值并将其存储到变量中(通过逗号分隔的值)
- IE9状态/存储选择选项将不会追加