HTML:
<select name="change" id="change">
<option value="1">Design 1</option>
<option value="2">Design 2</option>
<option value="3">Design 3</option>
<option value="4">Design 4</option>
</select>
JS:
function createCookie(name,value,days) {
var expires = "";
if (days) {
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
expires = "; expires="+date.toGMTString();
}
document.cookie = name+"="+value+expires+"; path=/";
}
function readCookie(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
}
return null;
}
function chooseStyle() {
var title = document.getElementById("change").value;
var links = document.getElementsByTagName("link");
createCookie("style", title, 30);
for(i = 0; i < links.length; i++) {
var a = links[i];
if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
a.disabled = true;
if(a.getAttribute("title") == title) a.disabled = false;
}
}
}
function init()
{
var styleCookie = document.getElementById("change");
styleCookie.onchange = chooseStyle;
var style = readCookie("style");
if (style !== null){
document.getElementById("change").value = style;
}
chooseStyle();
}
window.onload = init;
我一直在努力创建一个网站,您可以通过下拉菜单选择四种不同的CSS设计,然后将其保存为cookie或本地存储,以便在您重新访问网站时设计相同。
我需要一些帮助才能弄清楚,当你第一次访问该页面时,它会被加载默认设计(设计1),我认为它不会出现这种情况。真的很感激一些建议!
答案 0 :(得分:0)
在您的初始化中,您只需添加其他内容:
if (style !== null){
document.getElementById("change").value = style;
} else {
document.getElementById("change").value = "1";
}