2012-06-06 42 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.

相關問題