关于实现网站换肤

虽然不是我写的代码,但是我把所有的注释都加上去了,代码应该很好懂了。
这我即将做的一个项目需要用到的一个JQuery。分享给大家。

/**
* Styleswitch stylesheet switcher built on jQuery
* Under an Attribution, Share Alike License
* By Kelvin Luck ( http://www.kelvinluck.com/ )
**/
//页面解析完成后立即使用这个,里面的function是一个匿名函数,应用包含styleswitch的class,设定点击事件
//获取styleswitch中的rel属性里的值
$(document).ready(function() {
$('.styleswitch').click(function()
{
switchStylestyle(this.getAttribute("rel"));
return false;
});
var c = readCookie('style');//css1
if (c) switchStylestyle(c);
});

function switchStylestyle(styleName)
{
//link元素中带有rel属性的并且值包含style的,并且包含title属性的
$('link[@rel*=style][@title]').each(function(i)
{
//this代表link元素的disabled属性,并且将这个属性设为true
this.disabled = true;
//判断title里的值是不与传过来的styleName相等,若相等,则disabled设为false
if (this.getAttribute('title') == styleName) this.disabled = false;
});
//调用createCookie并且传3个参数,‘style’,css1,365天
createCookie('style', styleName, 365);
}

// cookie functions http://www.quirksmode.org/js/cookies.html
function createCookie(name,value,days)
{
if (days)
{
var date = new Date();
//当前一年加上一年比如现在是2010 那这里显示的就是2011 Sun, 18 Sep 2011 10:48:37 GMT
date.setTime(date.getTime()+(days*24*60*60*1000));
//获取格林威治时间值 ;expries=Sun, 18 Sep 2011 10:48:37 GMT
var expires = "; expires="+date.toGMTString();
}
else var expires = "";
//设置cookie style=css1+时间
document.cookie = name+"="+value+expires+"; path=/";
//cookie= style=css1;expries=Sun, 18 Sep 2011 10:48:37 GMT;path=/
}
function readCookie(name)
{
//nameEQ="style="
var nameEQ = name + "=";
//ca[0]= "style=css1" ca[1]="expries=Sun, 18 Sep 2011 10:48:37 GMT" ca[2]="path=/"
var ca = document.cookie.split(';');
//循环遍历ca
for(var i=0;i < ca.length;i++)
{
var c = ca;
while (c.charAt(0)==' ') c = c.substring(1,c.length);//style=css1 expries=Sun, 18 Sep 2011 10:48:37 GMT
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);//css1
}
return null;
}
function eraseCookie(name)
{
createCookie(name,"",-1);
}
// /cookie functions
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值