我在使用 jquery 1.7.1 版和 jquery cookie 1.4.1 版时遇到了同样的问题
这让我发疯,所以我决定深入研究源代码并找出问题所在。
这是 $.removeCookie 的定义
$.removeCookie = function (key, options) {
if ($.cookie(key) === undefined) { // this line is the problem
return false;
}
// Must not alter options, thus extending a fresh object...
$.cookie(key, '', $.extend({}, options, { expires: -1 }));
return !$.cookie(key);
};
正如您所看到的,当函数检查 cookie 是否存在时,它没有考虑选项对象。因此,如果您在与尝试删除的 cookie 不同的路径上,该功能将失败。
一些解决方案:
升级 Jquery Cookie。最新版本甚至没有进行完整性检查。
或将此添加到您准备好的文档中
$.removeCookie = function (key, options) {
if ($.cookie(key, options) === undefined) { // this line is the fix
return false;
}
// Must not alter options, thus extending a fresh object...
$.cookie(key, '', $.extend({}, options, { expires: -1 }));
return !$.cookie(key);
};
或者在删除 cookie 时执行以下操作:
$.cookie('cookie-name', '', { path: '/my/path', expires:-1 });