2012-06-06 40 views
11

试图获得cookie的值,如果它设置并更新divcookie值,否则生成80-100之间的随机数,将其设置为cookie,然后更新div对象函数(a,b){return new e.fn.init(a,b,h)}没有方法'cookie'

即时得到错误:

Object function (a,b){return new e.fn.init(a,b,h)} has no method 'cookie' 

继承人我的代码:

$(document).ready(function(){ 

    var thecounter = ''; 

    if ($.cookie('thecounter') == null) 
    { 
     thecounter = $.randomBetween(80, 100); 
     $.cookie('thecounter', thecounter, { path: '/' }); 
    } 
    else 
    { 
     thecounter = $.cookie('thecounter'); 
    } 

    $('dd-counter-num').html(thecounter); 

    setTimeout(newtime, 5000); 

}); 

function newtime() { 

    var newtime = $.cookie('thecounter') + $.randomBetween(1, 2); 
    $.cookie('thecounter', newtime, { path: '/' }); 
    $('dd-counter-num').html(newtime); 
    setTimeout(newtime, 5000); 

} 
+0

看起来像这是你正在寻找的插件:https://github.com/carhartl/jquery-cookie我想添加它,因为这也帮助了我。 –

回答

14

没有cookie方法标准jQuery

也许代码需要jquery-cookie或其他插件?

快乐编码。

+1

我认为这个方法是从我读过的教程中构建到jquery中的,没有包含插件URL。我发现了这个插件,它现在可以工作。谢谢。 – scarhand

+0

hi scarhand ...从哪里获得了jquery-cookie插件? – antnewbee

+0

这里是插件:http://plugins.jquery.com/cookie/ – ezawadzki

2

确保您在任何标准jQuery引用后引用jquery.cookie.js。

I encountered this error when editing a page using Foundation . It was first loading jquery, then jquery.cookie.js, then the foundation.min.js file. I didn't realize was the custom foundation.min.js file already had jquery built-in, so executing JQuery again was causing the cookie javascript to fail.

相关问题