<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
<head>
<title>网页换肤</title>
<link id="myCss" href="orange.css" rel="stylesheet">
</head>
<script>
function writeCookie(csspath) {
var today = new Date();
var expires = new Date();
//有效期为30天
expires.setTime(today.getTime() + 1000 * 60 * 60 * 24 * 30);
var str = "cssPath=" + csspath + ";expires=" + expires.toGMTString()
+ ";";
document.cookie = str;
}
//读取cookie信息的方法
function readCookie(cookieName) {
var search = cookieName = "=";
if (document.cookie.length > 0) {
offset = document.cookie.indexOf(search);
if (offset != -1) {
offset += search.length;
end = document.cookie.indexOf(";", offset);
if (end == -1) {
end = document.cookie.length;
return unescape(document.cookie.substring(offset, end));
}
}
}
}
//onclick事件调用的方法
function change(type) {
if (type == "orange") {
document.getElementById("myCss").href = "orange.css";
writeCookie("orange.css");
}
if (type == "gray") {
document.getElementById("myCss").href = "gray.css";
writeCookie("gray.css");
}
}
</script>
<body>
<a href="#" onClick="change('orange')">[橘色经典]</a>
<a href="#" onClick="change('gray')">[灰色畅想]</a>
</body>
</html>
gray.css
body{
background-color:gray;
}
orange.css
body{
background-color:orange;
}
说明:因为在进行换肤操作的时候,改变了网页的样式href
如果关闭浏览器之后样式就会恢复到原先的样子。
所以这里要使用cookie将修改的信息进行保存。
但是这里的代码只实现了换肤的功能,cookie保存信息还是没有实现....