转:基于jquery点击切换样式并且可以记忆cookie

这个网页特效基于jquery的样式切换jquery插件,点击按钮链接切换样式并且可以记忆cookie的jquery插件styleswitch.js,没有清除cookie的情况下他会一直保持上一次的选择状态。

必须在 head 区域引用 jquery.min.js 和styleswitch.js 示例如下:
 <script type="text/javascript" src="jquery.min.js"></script>
 <script type="text/javascript" src="styleswitch.js"></script>

要切换的样式自己设置。

你可以自行配置 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);
// cookie functions http://www.quirksmode.org/js/cookies.html
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);
}
// /cookie functions
 

魔兽就是毒瘤,大家千万不要玩。
原文地址:https://www.cnblogs.com/tracy/p/1768373.html