【发布时间】:2015-07-28 20:41:42
【问题描述】:
我正在制作一个页面,用户可以通过下拉菜单更改页面上的多个外观/样式,这会更改 css 文件名。我已经设法通过下拉菜单更改样式表,但我现在想将当前的 css 文件保存在本地存储中。例如,如果用户更改为 stylesheet3.css(通过下拉菜单),则此样式/外观将在用户下次访问页面时设置。这是我的代码:
HTML:
<link id="myStyleSheet" href="stylesheet1.css" rel="stylesheet" type="text/css"> I put an id in stylesheet
<div id="changeLook">
Change style:
<select id="changeStyle" onchange="changeStyleFunction()">
<option value="stylesheet1.css">Style 1</option>
<option value="stylesheet2.css">Style 2</option>
<option value="stylesheet3.css">Style 3</option>
<option value="stylesheet4.css">Style 4</option>
</select>
</div>
JavaScript:
function changeStyleFunction() {
var href;
var value = document.getElementById("changeStyle").value;
localStorage.setItem("GetData" , value); //Here is my biggest problem???
href = value;
document.getElementById('myStyleSheet').href = href;
}
如何使当前样式表(值)进入 localStore?我是 Local Storages 的新手……谢谢!
【问题讨论】:
-
代码看起来不错..每次重新加载页面时都需要读取localstorage值...
-
我该怎么做?任何提示?谢谢!
标签: javascript css local-storage