【发布时间】:2012-08-11 13:28:14
【问题描述】:
我找到了以下 JS 代码(根本不是我的,我不相信。感谢 Michael Regan,www.owt4nowt.ca)
所以,代码:
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script>
var key_value = "myTestCookie=true";
var foundCookie = 0;
// Get all the cookies from this site and store in an array
var cookieArray = document.cookie.split(';');
for(var i=0;i < cookieArray.length;i++)
{
var checkCookie = cookieArray[i];
while (checkCookie.charAt(0)==' ')
{
checkCookie = checkCookie.substring(1,checkCookie.length);
}
if (checkCookie.indexOf(key_value) == 0)
{
alert("Found Cookie ");
foundCookie = 1;
}
}
if ( foundCookie == 0)
{
document.cookie = key_value;
alert("Setting Cookie");
}
</script>
警报正常工作。
但是,当 cookie 被识别时,我删除了警报:
if (checkCookie.indexOf(key_value) == 0)
{
foundCookie = 1;
}
我用 addClass 函数替换了“设置 Cookie”警报,或者我是这么认为的。
if ( foundCookie == 0)
{
document.cookie = key_value;
$('.someClassHere').addClass("newClass");
}
问题是该功能根本不起作用。如果只留给alerts也没问题,但是addClass函数完全被忽略了。
addClass 函数有语法错误吗?
【问题讨论】:
-
您错过了一个实际问题!有什么不工作吗?你收到错误了吗?
-
我更新了帖子,感谢您对含糊不清的提醒
-
看起来您正在尝试使用 jQuery - 您是否包含了 jQuery 库?
-
我已包含指向 Google 1.7.2 链接的链接
-
您能否在问题中添加您如何链接到 jQuery。
标签: javascript session cookies addclass