基于 cookie 的 Javascript 条件
我试图设置一些条件语句来检测页面加载时是否设置了 cookie,并根据该 cookie 的值将一些类应用于 div:
<script>
$(function(){
if ($.cookie('view_size', 'large')) {
$('#primary').removeClass('medium_content');
$('#primary').addClass('large_content');
};
if ($.cookie('view_size', 'medium')) {
$('#primary').removeClass('large_content');
$('#primary').addClass('medium_content');
};
if ($.cookie('view_size', 'small')) {
$('#primary').removeClass('large_content');
$('#primary').removeClass('medium_content');
};
});
</script>
我正在使用 jquery cookie 插件,并且我知道 cookie 是成功设置,所以问题一定与我设置这些条件语句的方式有关。有什么想法吗?
Im trying to set up some conditional statements that detect whether or not a cookie has been set when the page loads and applies some classes to a div based on the value of that cookie:
<script>
$(function(){
if ($.cookie('view_size', 'large')) {
$('#primary').removeClass('medium_content');
$('#primary').addClass('large_content');
};
if ($.cookie('view_size', 'medium')) {
$('#primary').removeClass('large_content');
$('#primary').addClass('medium_content');
};
if ($.cookie('view_size', 'small')) {
$('#primary').removeClass('large_content');
$('#primary').removeClass('medium_content');
};
});
</script>
I am using the jquery cookie plugin and I know the cookies are getting successfully set, so the issue must have to do with the way I have set up these conditional statements. Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我相信语法
是创建 cookie 的方式,并不意味着检查 cookie 的值。我相信你想要
I believe that the syntax
is the way that you create a cookie, and isn't meant to check the value of a cookie. I believe you want