问题 对象函数(a,b){return new e.fn.init(a,b,h)}没有方法'cookie'


试图获得a的价值 cookie 如果它的设置和更新a div 随着 cookie 值,否则生成80-100之间的随机数,设置为 cookie,然后更新 div

我得到错误:

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);

}

5168
2018-06-06 18:33


起源

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


答案:


没有 cookie 方法 标准  jQuery的

也许代码需要 jQuery的饼干 还是另一个插件?

快乐的编码。


14
2018-06-06 18:37



我认为这个方法是从我阅读的教程中构建到jquery中的,该教程不包含插件URL。我发现了这个插件,它现在有效。谢谢。 - scarhand
嗨scarhand ...你从哪里得到jquery-cookie插件? - antnewbee
这是插件: plugins.jquery.com/cookie - emilie zawadzki


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

使用时编辑页面时遇到此错误 基础。那是第一次加载jquery   jquery.cookie.js,然后是foundation.min.js文件。我没有意识到是习俗   foundation.min.js文件已经内置了jquery,因此再次执行JQuery会导致cookie   javascript失败。


2
2017-09-04 15:47





1)从GitHub下载此插件: https://github.com/carhartl/jquery-cookie/blob/master/jquery.cookie.js

2)将它放在脚本文件夹中。

3)将其包含在您的网站上:

<script src="~/Scripts/jquery.cookie.js" type="text/javascript"></script>

为我工作。


0
2018-05-24 03:40