0

我正在为特定域设置 myCookie,当设置 cookie 时,我想隐藏内容,但显示/隐藏 IF 语句根本不起作用。

我想使用 jquery .cookie 插件,但这是我目前所拥有的。写 cookie 确实有效,我只是无法让隐藏功能正常工作。

var cookieName = 'myCookie';
var cookieValue = 'myCookie';
var myDate = new Date();
myDate.setMonth(myDate.getMonth() + 12);
document.cookie = cookieName +"=" + cookieValue + ";expires=" + myDate 
                  + ";domain=.example.com;path=/";

$(document).ready(function(){
    if ($.cookieName('myCookie') >= 0) {
        $('.myContent').hide();
    }
});

HTML

<div class="myContent">
hide this content if the cookie is set
</div>
otherwise show this content if you don't have the required cookie

对此的任何帮助将不胜感激!

4

2 回答 2

2

我给你举了一个例子:

<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=/";

萨卢多斯 ;)

于 2013-03-27T16:07:56.240 回答
1

首先,下载https://github.com/carhartl/jquery-cookie并在您的页面中引用。

然后,就像将您的if声明更改为:

if ($.cookie("myCookie") !== undefined)
{
    $(".myContent").hide();
}

如果你愿意,你也可以$.cookie用来设置 cookie;README.md 有示例。

于 2013-03-27T15:58:46.833 回答