web风格样式动态切换并cookie记忆

(1)

(2)html

 <ul class="skinner fr">
  <li class="fl"><a href="#" rel="style_blue" class="styleswitch skin skin_blue fl"></a></li>
  <li class="fl"><a href="#" rel="style_green" class="styleswitch skin skin_green fl"></a></li>
  <li class="fl"><a href="#" rel="style_red" class="styleswitch skin skin_red fl"></a></li>
  <li class="fl"><a href="#" rel="style_purple" class="styleswitch skin skin_purple fl"></a></li>
  <li class="clear"></li>
  </ul>

(3)styleswitch.js

(function($)
{
	$(document).ready(function() {
		$('.styleswitch').click(function()
		{
			switchStylestyle(this.getAttribute("rel"));
			return false;
		});
		var c = readCookie('style');
		if (c) switchStylestyle(c);
	});

	function switchStylestyle(styleName)
	{
		$('link[@rel*=style][title]').each(function(i) 
		{
			this.disabled = true;
			if (this.getAttribute('title') == styleName) this.disabled = false;
		});
		createCookie('style', styleName, 365);
	}
})(jQuery);
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 var 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;
}
function eraseCookie(name)
{
	createCookie(name,"",-1);
}

 ------------------rel 属性-------------------

rel 属性 -- rel属性,描述了当前页面与href所指定文档的关系.
  • rel属性通常出现在a,link标签中
  • 引用网址:http://www.dreamdu.com/xhtml/attribute_rel/
  • 属性值
    • alternate -- 定义交替出现的链接
    • appendix -- 定义文档的附加信息
    • bookmark -- 书签
    • canonical -- 规范网页是一组内容高度相似的网页的首选版本
    • chapter -- 当前文档的章节
    • contents
    • copyright -- 当前文档的版权
    • glossary -- 词汇
    • help -- 链接帮助信息
    • index -- 当前文档的索引
    • next -- 记录文档的下一页.(浏览器可以提前加载此页)
    • nofollow -- 不被用于计算PageRank
    • prev -- 记录文档的上一页.(定义浏览器的后退键)
    • section -- 作为文档的一部分
    • start -- 通知搜索引擎,文档的开始
    • stylesheet -- 定义一个外部加载的样式表
    • subsection -- 作为文档的一小部分
  • rel是relationship的英文缩写

原文地址:https://www.cnblogs.com/fx2008/p/2301753.html