用cookies實(shí)現(xiàn)的可記憶的樣式切換效果代碼下載
更新時(shí)間:2007年12月24日 14:44:09 作者:
比較不錯(cuò)的用cookies實(shí)現(xiàn)的可記憶的樣式切換效果,這個(gè)思路也在一定程序,方便客戶的長(zhǎng)期使用。
無(wú)刷新cookies切換樣式示例代碼實(shí)例主要用到的代碼
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>styleswitcher腳本之家-www.dbjr.com.cn </title>
<link title=red rel="stylesheet" type="text/css" href="red.css">
<LINK title=blue href="blue.css" type=text/css rel="alternate stylesheet">
<SCRIPT src="styleswitcher.js" type=text/javascript></SCRIPT>
<style>
<!--
#wrapper { font-size: 10px;width:100px; }
#left { width:20px; height:100px; }
#right { width:80px;float:right;background-color:#000000;;height:100px;color:#FFFFFF }
-->
</style>
</head>
<body>
<A onclick="setActiveStyleSheet('red');return false;" href="#">red</A>
<A onclick="setActiveStyleSheet('blue');return false;" href="#">blue</A>
<select name="changestyle" size="1">
<option value="red">red</option>
<option value="blue">blue</option>
</select><input type="button" value="變" onclick="setActiveStyleSheet(changestyle.value);return false;">
<div id="wrapper">
<div id="left">left</div>
<div id="right">right</div>
</div>
</body>
</html>
styleswitcher.js
// styleswitcher.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);
red.css
#left { background-color:#0000FF; float:right;}
red.css
#left { background-color:#FF0000;float:left; }
復(fù)制代碼 代碼如下:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>styleswitcher腳本之家-www.dbjr.com.cn </title>
<link title=red rel="stylesheet" type="text/css" href="red.css">
<LINK title=blue href="blue.css" type=text/css rel="alternate stylesheet">
<SCRIPT src="styleswitcher.js" type=text/javascript></SCRIPT>
<style>
<!--
#wrapper { font-size: 10px;width:100px; }
#left { width:20px; height:100px; }
#right { width:80px;float:right;background-color:#000000;;height:100px;color:#FFFFFF }
-->
</style>
</head>
<body>
<A onclick="setActiveStyleSheet('red');return false;" href="#">red</A>
<A onclick="setActiveStyleSheet('blue');return false;" href="#">blue</A>
<select name="changestyle" size="1">
<option value="red">red</option>
<option value="blue">blue</option>
</select><input type="button" value="變" onclick="setActiveStyleSheet(changestyle.value);return false;">
<div id="wrapper">
<div id="left">left</div>
<div id="right">right</div>
</div>
</body>
</html>
styleswitcher.js
復(fù)制代碼 代碼如下:
// styleswitcher.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);
red.css
復(fù)制代碼 代碼如下:
#left { background-color:#0000FF; float:right;}
red.css
復(fù)制代碼 代碼如下:
#left { background-color:#FF0000;float:left; }
相關(guān)文章
基于Bootstrap使用jQuery實(shí)現(xiàn)簡(jiǎn)單可編輯表格
這篇文章主要介紹了基于Bootstrap使用jQuery實(shí)現(xiàn)簡(jiǎn)單可編輯表格的相關(guān)資料,需要的朋友可以參考下2016-05-05JS開(kāi)發(fā)中基本數(shù)據(jù)類型具體有哪幾種
JS的數(shù)據(jù)類型包括基本數(shù)據(jù)類型、復(fù)雜數(shù)據(jù)類型和特殊數(shù)據(jù)類型,今天我們主要先講解一下基本數(shù)據(jù)類型。感興趣的朋友一起看看吧2017-10-10

javascript 正則表達(dá)式相關(guān)應(yīng)介紹
javascript 中幾個(gè)與正則表達(dá)式相關(guān)的應(yīng)用,本文將詳細(xì)介紹,需要的朋友可以參考下
2012-11-11 
JavaScript字符串操作的四個(gè)實(shí)用技巧
在制作前端頁(yè)面的過(guò)程中,經(jīng)常需要用到JavaScript進(jìn)行邏輯處理,很多時(shí)候都需要對(duì)字符串進(jìn)行操作,這篇文章主要給大家介紹了關(guān)于JavaScript字符串操作的四個(gè)實(shí)用技巧,需要的朋友可以參考下
2021-07-07 
JavaScript動(dòng)態(tài)修改彈出窗口大小的方法
這篇文章主要介紹了JavaScript動(dòng)態(tài)修改彈出窗口大小的方法,涉及javascript中window.open方法的使用技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下
2015-04-04