jQuery – 未捕获TypeError:$不是函数

我很困惑……我正在我的网站上工作并添加了一些jQuery。 经过测试,我发现jQuery没有做任何事情。 然后我尝试了我能想到的最简单但最明显的代码:

 $("html").click(function() { $("html").html(""); });  

但那也没有做任何事情。 所以我在js控制台中尝试了相同的代码并找回了这个错误:

 Uncaught TypeError: $ is not a function at :2:1 at Object.InjectedScript._evaluateOn (:848:140) at Object.InjectedScript._evaluateAndWrap (:891:34) at Object.InjectedScript.evaluate (:627:21) 

只需输入$ just即可返回“undefined”。

我调查并发现我不小心移动了我的Gemfile,也许服务器无法访问jquery-rails gem。 但在我将其移回并重新启动服务器后,问题并没有消失。

然后我想也许我的页面上有一些错误的代码阻止了jQuery的运行。 我删除了所有代码,因此它只是一个空白文档,运行它,并再次尝试,但我在控制台中得到了相同的错误。

我的应用程序负责人包括:

   true %>  

我的app/assets/javascripts/application.js包含//= require jquery这一行

bundle install返回“jquery-rails 4.1.1”。

我之前在同一页面上运行了大量的jQuery而没有任何问题。 看来我的服务器突然停止了解jQuery。 到底是怎么回事?

将它包装在jQuery函数中,如下所示:

 // Note the "$" inside function() - that's the key jQuery( document ).ready(function( $ ) { $("html").click(function() { $("html").html(""); }); }); 

确保在app/assets/application.js包含jquery:

 //= require jquery //= require jquery_ujs