jQuery-未捕获的TypeError:$不是函数 [英] jQuery - Uncaught TypeError: $ is not a function

查看:132
本文介绍了jQuery-未捕获的TypeError:$不是函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很困惑...我在自己的网站上工作,并添加了一些jQuery.经过测试,我发现jQuery没有执行任何操作.然后,我尝试了我能想到的最简单但可见的代码:

I am so confused...I was working on my site and added some jQuery. Upon testing, I found that the jQuery didn't do anything. I then tried the most simple yet visible code I could think of:

<script type="text/javascript">
  $("html").click(function() {
    $("html").html("");
  });
</script>

但是那也没有做任何事情.因此,我在js控制台中尝试了相同的代码,并返回了此错误:

but that didn't do anything either. So I tried that same code in the js console and got back this error:

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

,仅输入$只会返回"undefined".

and just entering $ just gives back "undefined".

我进行了调查,发现我不小心移动了Gemfile,也许服务器无法访问jquery-rails gem.但是当我将其移回并重新启动服务器后,问题并没有解决.

I investigated and found that I had accidentally moved my Gemfile, and that maybe the server couldn't access the jquery-rails gem. But after I moved it back and restarted the server, the problem didn't go away.

然后,我想我的页面上可能有一些错误的代码阻止了jQuery的运行.我删除了所有代码,使它只是一个空白文档,运行并再次尝试,但是在控制台中出现了相同的错误.

Then I thought maybe I had some faulty code on my page that was preventing the jQuery from running. I removed all the code so it was a just a blank document, ran it, and tried again, but I got the same errors in the console.

我的应用程序的head包括以下内容:

The head of my app includes this:

<head>
  <%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
</head>

我的app/assets/javascripts/application.js包括行//= require jquery

然后bundle install返回"jquery-rails 4.1.1".

And bundle install returns "jquery-rails 4.1.1".

在同一页面上,我之前运行过很多jQuery,没有任何问题.看来我的服务器突然停止了对jQuery的理解.到底会发生什么?

I've ran plenty of jQuery before on the same page with no issues. It seems my server suddenly just stopped understanding jQuery. What on earth could be going on?

推荐答案

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

Wrap it in a jQuery function, like this:

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

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

});

这篇关于jQuery-未捕获的TypeError:$不是函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆