简体   繁体   English

Javascript 条件基于 cookies

[英]Javascript conditionals based on cookies

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:我试图设置一些条件语句来检测页面加载时是否设置了 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>

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.我正在使用 jquery cookie 插件,并且我知道 cookies 已成功设置,因此问题必须与我设置这些条件语句的方式有关。 Any ideas?有任何想法吗?

I believe that the syntax我相信语法

$.cookie('view_size', 'large')

is the way that you create a cookie, and isn't meant to check the value of a cookie.是您创建 cookie 的方式,并不意味着检查 cookie 的值。 I believe you want我相信你想要

if ($.cookie('view_size')=='large')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM