jQuery通过写入cookie实现更换网页背景的方法
作者:bea
本文实例讲述了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>
本文实例讲述了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 class="skin"><ul class="skin_list"><li style="width:100px;text-align:right;">更换背景:</li></li><li class="red"></li><li class="black"></li><li class="blue"></li><li class="green"></li></ul></div>
</body>
</html>
更多关于jQuery相关内容感兴趣的读者可查看本站专题:《jQuery的cookie操作技巧总结》、《jQuery表格(table)操作技巧汇总》、《jQuery拖拽特效与技巧总结》、《jQuery扩展技巧总结》、《jQuery常见经典特效汇总》、《jQuery动画与特效用法总结》、《jquery选择器用法总结》及《jQuery常用插件及用法总结》
希望本文所述对大家jQuery程序设计有所帮助。
有用 | 无用
<!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 class="skin"><ul class="skin_list"><li style="width:100px;text-align:right;">更换背景:</li></li><li class="red"></li><li class="black"></li><li class="blue"></li><li class="green"></li></ul></div>
</body>
</html>
更多关于jQuery相关内容感兴趣的读者可查看本站专题:《jQuery的cookie操作技巧总结》、《jQuery表格(table)操作技巧汇总》、《jQuery拖拽特效与技巧总结》、《jQuery扩展技巧总结》、《jQuery常见经典特效汇总》、《jQuery动画与特效用法总结》、《jquery选择器用法总结》及《jQuery常用插件及用法总结》
希望本文所述对大家jQuery程序设计有所帮助。
有用 | 无用
猜你喜欢
您可能感兴趣的文章:
- 非常棒的jQuery图片轮播效果
- jQuery实现的倒计时效果实例小结
- jQuery实现漂亮实用的商品图片tips提示框效果(无图片箭头+阴影)
- jQuery实现无限往下滚动效果代码
- jQuery遍历json的方法分析
- jquery对dom节点的操作【推荐】
- Jquery attr()方法 属性赋值和属性获取详解
- 有关jquery与DOM节点操作方法和属性记录
- 快速解决jquery.touchSwipe左右滑动和垂直滚动条冲突
- 使用Object.defineProperty实现简单的js双向绑定
- jQuery实现下拉框左右移动(全部移动,已选移动)
- jQuery调用WebMethod(PageMethod) NET2.0的方法
- jQuery解决浏览器兼容性问题案例分析
- jQuery使用cookie与json简单实现购物车功能
- 在Html中使用Requirejs进行模块化开发实例详解
- jQuery基于json与cookie实现购物车的方法
- Window.Open打开窗体和if嵌套代码
- jQuery设置Cookie及删除Cookie实例分析
- jQuery获取cookie值及删除cookie用法实例