$ .cookie('name')不是函数?

Ton*_*bet 0 javascript cookies jquery

我正在尝试保存并使用cookie来了解viewStyle用户想要什么样的.这个饼干有一个swich.

这就是我想要更新cookie的方式

                $('body').on('click','.viewBy li a',function(){
                    if($('.titular').length > 0){
                         $.cookie('viewBy','titular',{expires:30});
                    }else{
                         $.cookie('viewBy','',{expires:30});
                    }
                });
Run Code Online (Sandbox Code Playgroud)

这就是我的检查方式

                if($.cookie('viewBy')){
                        $('.viewBy li').toggleClass('active');
                        $('.recetaTitular').addClass('titular');
                } 
Run Code Online (Sandbox Code Playgroud)

但我得到这个firebug错误:

$.cookie is not a function
[Parar en este error]   
Run Code Online (Sandbox Code Playgroud)

我试过了

                if(typeof $.cookie('viewBy') != 'undefined' &&($.cookie('viewBy'))){
                        $('.viewBy li').toggleClass('active');
                        $('.recetaTitular').addClass('titular');
                } 
Run Code Online (Sandbox Code Playgroud)

但得到同样的错误

Pab*_*dez 5

$.cookie不是jQuery函数.你需要包含一个插件,也许这个:

https://github.com/carhartl/jquery-cookie

之后您加载jQuery插件.