Jquery cookies操作
jQuery操作cookie的插件,大概的使用方法如下
$.cookie('the_cookie'); //读取Cookie值
$.cookie('the_cookie', 'the_value'); //设置cookie的值
$.cookie('the_cookie', 'the_value', {expires: 7, path: '/', domain: 'jquery.com', secure: true});//新建一个cookie 包括有效期 路径 域名等
$.cookie('the_cookie', 'the_value'); //新建cookie
$.cookie('the_cookie', null); //删除一个cookie
设置一个名称为exist,值为exist的cookie:
$.cookie("exist", "exist");
设置一个名称为exist,值为exist的cookie,同时设置过期时间(expires属性)为7天:
$.cookie("exist", "exist", { expires: 7 });
设置一个名称为exist,值为exist的cookie,设置过期时间(expires属性)为7天,同时设置cookie的path属性为”/admin”
$.cookie("exist", "exist", { path: '/admin', expires: 7 });
读取Cookie:
读取名称为exist的cookie值:
alert( $.cookie("exist") );
删除cookie:
$.cookie("example", null);
例如:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<script src="../Scripts/jquery-1.4.1.min.js"></script>
<script src="../Scripts/jquery.cookie.js"></script>
<script type="text/javascript">
$(document).ready(function () {
var i = 1;
//当点击按钮的时候隐藏《p》
$("#clk_Submit").click(function () {
//判断是否存在cookies对象
if ($.cookie("exist") == null || $.cookie("exist") == "undefined") {
//创建cookies对象并且给其赋值,设置了生命周期为7天
$.cookie("exist", i, { expires: 7 });
}
//获取cookies值
i = $.cookie("exist");
//判断是否为0
if (i % 2 != 0) {
$("p").hide();
}
else {
$("p").show();
}
i++;
//保存到cookies里面
$.cookie("exist", i);
})
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2>This is a heading</h2>
<p>This is a paragraph.</p>
<p>This is another paragraph.</p>
<button type="button" id="clk_Submit">Click me</button>
</div>
</form>
</body>
</html> 相关推荐
houmenghu 2020-11-17
kentrl 2020-11-10
jincheng 2020-09-01
Blueberry 2020-08-15
xclxcl 2020-08-03
zmzmmf 2020-08-03
阳光之吻 2020-08-03
PkJY 2020-07-08
hzyuhz 2020-07-04
89407707 2020-06-27
服务器端攻城师 2020-06-26
阳光岛主 2020-06-25
笨重的蜗牛 2020-06-20
xuanwenchao 2020-06-14
Lophole 2020-06-13
明瞳 2020-06-12
songerxing 2020-06-11