无刷新cookies切换样式示例代码实例主要用到的代码
复制代码 代码如下:
styleswitcher脚本之家-www.jb51.net
red
blue
red
blue
left
right
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 {
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; }