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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10920055/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 09:57:38  来源:igfitidea点击:

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

jquerycookies

提问by scarhand

trying to get the value of a cookieif its set and update a divwith the cookievalue, otherwise generate a random number between 80-100, set that as the cookie, and then update the div.

尝试获取 a 的值,cookie如果其设置并div使用该cookie值更新 a ,否则生成 80-100 之间的随机数,将其设置为cookie,然后更新div.

im getting the error:

我收到错误:

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

heres my code:

继承人我的代码:

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

}

回答by

There is no cookiemethod in standardjQuery.

标准jQuery 中没有cookie方法。

Perhaps the code requires on jquery-cookieor another plugin?

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

Happy coding.

快乐编码。

回答by James Lawruk

Ensure you are referencing jquery.cookie.js after any standard jquery references.

确保在任何标准 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.

使用Foundation编辑页面时遇到此错误。它首先加载 jquery,然后是 jquery.cookie.js,然后是foundation.min.js 文件。我没有意识到自定义的foundation.min.js 文件已经内置了jquery,所以再次执行JQuery 会导致cookie javascript 失败。

回答by Arman Bimatov

1) Download this plugin from GitHub: https://github.com/carhartl/jquery-cookie/blob/master/jquery.cookie.js

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

2) Put it in your scripts folder.

2)把它放在你的脚本文件夹中。

3) Include it on your website:

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

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

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

Worked for me.

为我工作。