我有一个代码可以在Firefox和IE中更改样式表并将其保存在Cookie中,但它在Chrome中不起作用。
这是我使用的代码:
<head>
<link href="default.css" title="default" rel="stylesheet" type="text/css" />
<link href="theme1.css" title="theme1" rel="stylesheet" type="text/css" />
<link href="theme2.css" title="theme2" rel="stylesheet" type="text/css" />
<link href="theme3.css" title="theme3" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="javascript.js"></script>
</head>
<body>
<ul id="dropdown">
<li> Choose theme
<ul>
<li id="stylesheet1" > <a href="#"> Default </a></li>
<li id="stylesheet2" > <a href="#"> Theme 1 </a></li>
<li id="stylesheet3" > <a href="#"> Theme 2 </a></li>
<li id="stylesheet4" > <a href="#"> Theme 3 </a></li>
</ul>
</li>
</ul>
</body>
javascript.js包含:
function setActiveStyleSheet(title) {
var i, a, main;
for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
a.disabled = true;
if(a.getAttribute("title") == title) a.disabled = false;
}
}
}
function getActiveStyleSheet() {
var i, a;
for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") && !a.disabled) return a.getAttribute("title");
}
return null;
}
function getPreferredStyleSheet() {
var i, a;
for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
if(a.getAttribute("rel").indexOf("style") != -1
&& a.getAttribute("rel").indexOf("alt") == -1
&& a.getAttribute("title")
) return a.getAttribute("title");
}
return null;
}
function createCookie(name,value,days) {
if (days) {
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = "; expires="+date.toGMTString();
}
else expires = "";
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;
}
window.onload = function(e) {
var cookie = readCookie("style");
var title = cookie ? cookie : getPreferredStyleSheet();
setActiveStyleSheet(title);
}
window.onunload = function(e) {
var title = getActiveStyleSheet();
createCookie("style", title, 365);
}
var cookie = readCookie("style");
var title = cookie ? cookie : getPreferredStyleSheet();
setActiveStyleSheet(title);
function initate()
{
document.getElementById("stylesheet1").onclick = function() {
setActiveStyleSheet("default");
return false
};
document.getElementById("stylesheet2").onclick = function() {
setActiveStyleSheet("theme1");
return false
};
document.getElementById("stylesheet3").onclick = function() {
setActiveStyleSheet("theme2");
return false
};
document.getElementById("stylesheet4").onclick = function() {
setActiveStyleSheet("theme3");
return false
}
}
window.onload = initate;
它可以在Chrome中更改样式表,但是当我重新加载页面时,它不像IE和Firefox那样保留所选的样式表。我无法理解为什么它在Chrome中不起作用。我是javascript的新手,我使用的大部分代码都不是由我编写的,而是来自不同的地方,所以我不太了解其中的大部分内容。
答案 0 :(得分:0)
您正在覆盖以前的onclick处理程序,只执行第二个处理程序:
document.getElementById("stylesheet1").onclick = function() {
createCookie(T1, style, 7);
};
...
var style1 = document.getElementById("stylesheet1");
style1.onclick = function () {
setActiveStyleSheet("default");
return false;
};
要将关于使用jQuery的注释放到透视图中,整个initiate
函数(目前大约50行)可以通过这种方式重写,大约7行:
$("li a").click(function () {
# Turn our text ("Theme 1") into the name of the sheet ("theme1")
var sheetName = $(this).text().toLowerCase().replace(/ /g, "");
createCookie(T1, sheetName, 7)
setActiveStyleSheet(sheetName);
});