我给你举了一个例子:
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript" src="jquery.cookie.js"></script>
<script type="text/javascript">
$.cookie('myCookie', 25);
$(document).ready(function(){
if ($.cookie('myCookie') >= 0) {
$('.myContent').hide();
}
});
</script>
</head>
<body>
<div class="myContent">
hide this content if the cookie is set
</div>
</body>
</html>
这个例子隐藏了 div,因为 25 > 0。
在其他情况下:
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript" src="jquery.cookie.js"></script>
<script type="text/javascript">
$.cookie('myCookie', 25);
$(document).ready(function(){
if ($.cookie('myCookie') >= 26) {
$('.myContent').hide();
}
});
</script>
</head>
<body>
<div class="myContent">
hide this content if the cookie is set
</div>
</body>
</html>
现在我更改了 if 条件和它显示的 div myCookie = 25 并且它小于 26(因此它在两种情况下都有效)。
------------------------------已编辑------- ---------------
Javascript版本:
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=/"; //replace this line
}
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);
}
Javascript 创建 cookie 调用:
createCookie('ppkcookie','testcookie',7) //name_of_cookie,value,num_days
读取 cookie:
var x = readCookie('ppkcookie1')
完整说明:http ://www.quirksmode.org/js/cookies.html
在 createCookie 函数中:
document.cookie = name+"="+value+expires+"; path=/"; //replace this line
//with this one adding domain
document.cookie = name+"="+value+expires=" + ";domain=.example.com;path=/";
萨卢多斯 ;)