我正在try 使用jQuery设置cookie:

$.cookie("testCookie", "hello");
alert($.cookie("testCookie"));

But when I load my page, I receive the error "$.cookie is not a function". Here is what I know:

  • 我已经下载了jQuery cookie插件here.
  • 我先链接到jQuery,然后链接到cookie插件.
  • jQuery和jQuery都有.cookie正在正确加载200个Ok.

I have looked at several other answers (here and here among others), to which most people suggested renaming the cookie.js file. I have renamed my cookie file "jquery.cookeee.js" but the results are the same.

Any ideas on what is going on here?

如果有帮助,我将在MVC4中创建一个web应用程序.

推荐答案

以下是我遇到的所有可能的问题/解决方案:

1. Download the cookie plugin

$.cookie is not a standard jQuery function and the plugin needs to be downloaded here. Make sure to include the appropriate <script> tag where necessary (see next).

2.在cookie插件之前包含jQuery

当包含cookie脚本时,确保首先包含jQuery,然后是cookie插件.

<script src="~/Scripts/jquery-2.0.3.js" type="text/javascript"></script>
<script src="~/Scripts/jquery_cookie.js" type="text/javascript"></script>

3. Don't include jQuery more than once

This was my problem. Make sure you aren't including jQuery more than once. If you are, it is possible that:

  1. jQuery loads correctly.
  2. Cookie插件加载正确.
  3. 第二次包含jQuery将覆盖第一次,并销毁cookie插件.

For anyone using ASP.Net MVC projects, be careful with the default javascript bundle inclusions. My second inclusion of jQuery was within one of my global layout pages under the line @Scripts.Render("~/bundles/jquery").

4. Rename the plugin file to not include ".cookie"

In some rare cases, renaming the file to something that does NOT include ".cookie" has fixed this error, apparently due to web server issues. By default, the downloaded script is titled "jquery.cookie.js" but try renaming it to something like "jquery_cookie.js" as shown above. More details on this problem are here.

Jquery相关问答推荐

Ajax 替换而不是追加

如何使用 JQuery Select 没有特定子元素的元素

获取单选按钮组的值

jQuery获取表单字段值

如何在 jQuery 中存储全局值(不一定是全局变量)?

javascript:检测滚动结束

按值对 Html Select 的选项进行排序的最有效方法是什么,同时保留当前选定的项目?

使用带有 HTML 表格的 jQuery UI 可排序

在jQuery中添加ID?

如何通过 DOM 容器访问 Highcharts 图表?

如何使用 JQuery $.scrollTo() 函数滚动窗口

在jQuery中获取CSS规则的百分比值

如何使用 javascript 展开和折叠

将数据发布到 JsonP

Fancybox 不适用于 jQuery v1.9.0 [ f.browser 未定义 / 无法读取属性 'msie' ]

jQuery attr 与props ?

表单 propType 失败:您在没有 `onChange` 处理程序的情况下向表单字段提供了 `value` props

粘性侧边栏:向下滚动时固定在底部,向上滚动时固定在顶部

小于 10 给数字加 0

使用 Ajax 下载并打开 PDF 文件