手机
当前位置:查字典教程网 >编程开发 >Javascript教程 >jQuery通过写入cookie实现更换网页背景的方法
jQuery通过写入cookie实现更换网页背景的方法
摘要:本文实例讲述了jQuery通过写入cookie实现更换网页背景的方法。分享给大家供大家参考,具体如下:jquery更换网页背景(写入cook...

本文实例讲述了jQuery通过写入cookie实现更换网页背景的方法。分享给大家供大家参考,具体如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>jquery更换网页背景(写入cookie)</title> <style type="text/css"> body,p,ul{margin:0;padding:0;} ul{float:right;height:40px;margin-top:20px;margin-right:20px;} ul li{list-style-type:none;float:left;width:20px;height:20px;margin-left:10px;cursor:pointer;} .skin{height:40px;position:fixed;background:#fff;border-bottom:solid 1px #cccc;top:0;left:0;width:100%;} .red{background:#F06;}/*红色*/ .black{background:#000;}/*黑色*/ .blue{background:#09F;}/*蓝色*/ .green{background:#093;}/*绿色*/ </style> <script type="text/javascript" src="http://lib.sinaapp.com/js/jquery/1.8.2/jquery.min.js"></script> <script type="text/javascript"> $(document).ready(function(){ //为了安全 google chrome 等浏览器是禁止本地文件写Cookie的即file:///F:/Lord%20community/lrtk/Untitled-2.html这样的以file开头的是不能写本地文件的 var cookieClass = getCookie('class');//读取需要缓存的对象。 $("body").attr("class",cookieClass);// $(".skin_list li").each(function(){ $(this).click(function(){ var className=$(this).attr("class");//保存当前选择的类名 $("body").attr("class",className,30);//把选中的类名给body function SetCookie(name,value,day)//两个参数,一个是cookie的名子,一个是值 { var exp = new Date(); //new Date("December 31, 9998"); exp.setTime(exp.getTime() + day*24*60*60*1000); document.cookie = name + "="+ escape (value) + ";expires=" + exp.toGMTString(); } SetCookie("class",className,30); }) }); }); function getCookie(name)//取cookies函数 { 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; } </script> </head> <body> <div><ul><li>更换背景:</li></li><li></li><li></li><li></li><li></li></ul></div> </body> </html>

希望本文所述对大家jQuery程序设计有所帮助。

【jQuery通过写入cookie实现更换网页背景的方法】相关文章:

jQuery实现延迟跳转的方法

JavaScript实现鼠标拖动效果的方法

JQuery实现动态添加删除评论的方法

jQuery实现转动随机数抽奖效果的方法

jQuery实现将页面上HTML标签换成另外标签的方法

javascript实现点击后变换按钮显示文字的方法

JQuery+CSS实现图片上放置按钮的方法

jquery简单实现外部链接用新窗口打开的方法

jQuery替换textarea中换行的方法

Javascript实现div层渐隐效果的方法

精品推荐
分类导航