Javascript 未捕获的 ReferenceError:$ 未定义?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2075337/
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-22 22:40:32  来源:igfitidea点击:

Uncaught ReferenceError: $ is not defined?

javascriptjqueryreferenceerror

提问by Olivers

How come this code throws an

这段代码怎么会抛出一个

Uncaught ReferenceError: $ is not defined

未捕获的 ReferenceError: $ 未定义

when it was OK before?

以前什么时候好?

$(document).ready(function() {
    $('#tabs > ul').tabs({ fx: { opacity: 'toggle' } });
    $('#featuredvid > ul').tabs();
});

Results in tabs don't close anymore.

选项卡中的结果不再关闭。

jQuery is referenced in the header:

标题中引用了 jQuery:

<script language="JavaScript" type="text/javascript" src="<?php echo get_option('siteurl') ?>/js/sprinkle.js"></script>
<script language="JavaScript" type="text/javascript" src="<?php echo get_option('siteurl') ?>/js/jquery-1.2.6.min.js"></script>
<script language="JavaScript" type="text/javascript" src="<?php echo get_option('siteurl') ?>/js/jquery-ui-personalized-1.5.2.packed.js"></script>

回答by Jeremy

You should put the references to the jquery scripts first.

您应该首先放置对 jquery 脚本的引用。

<script language="JavaScript" type="text/javascript" src="/js/jquery-1.2.6.min.js"></script>
<script language="JavaScript" type="text/javascript" src="/js/jquery-ui-personalized-1.5.2.packed.js"></script>
<script language="JavaScript" type="text/javascript" src="/js/sprinkle.js"></script>

回答by Open Source

You are calling the ready function before the jQuery JavaScript is included. Reference jQuery first.

您是在包含 jQuery JavaScript 之前调用 ready 函数。首先参考jQuery。

回答by Marlin Mixon

This is what solved it for me. Originally I went to Google and copied and pasted their suggested snippet for jQuery on their CDN page:

这就是为我解决的问题。最初我去谷歌并在他们的 CDN 页面上复制并粘贴了他们建议的 jQuery 片段:

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>

The snippet does not include the HTTP:or HTTPS:in the srcattribute but my browser, FireFox, needed it so I changed it to: edit: this worked for me with Google Chrome as well

该代码段在属性中不包含HTTP:HTTPS:src但我的浏览器 FireFox 需要它,所以我将其更改为:编辑:这对我来说也适用于 Google Chrome

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>

Thenit worked.

然后它起作用了。

回答by Jnanaranjan

If your custom script is loaded before the jQuery plugin is loaded to the browser then this type of problem may occur. So, always keep your own JavaScript or jQuery code after calling the jQuery plugin so the solution for this is :

如果您的自定义脚本在 jQuery 插件加载到浏览器之前加载,则可能会出现此类问题。因此,在调用 jQuery 插件后始终保留您自己的 JavaScript 或 jQuery 代码,因此解决方案是:

First add the link to the jQuery file hosted at GoogleApis or a local jQuery file that you will download from http://jquery.com/download/and host on your server:

首先将链接添加到托管在 GoogleApis 上的 jQuery 文件或您将从http://jquery.com/download/下载并托管在您的服务器上的本地 jQuery 文件:

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>

or any plugin for jQuery. Then put your custom script file link or code:

或任何 jQuery 插件。然后把你的自定义脚本文件链接或代码:

<script src="js/custom.js" type="text/javascript"></script>

回答by Jnanaranjan

In my case I was putting my .jsfile before the jQuery script link, putting the .jsfile after jQuery script link solved my issue.

就我而言,我将.js文件放在jQuery 脚本链接之前,将.js文件放在jQuery 脚本链接之后解决了我的问题。

<script src="http://code.jquery.com/jquery-1.10.2.js"></script>
<script src="http://code.jquery.com/ui/1.11.2/jquery-ui.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
<script src="exponential.js"></script>

回答by Sudipta Chatterjee

Okay, my problem was different - it was Document Securitymodel in Chrome.

好的,我的问题是不同的 - 它是Chrome 中的文档安全模型。

Looking at the answers here, it was obvious that I was somehow not loading my jquery files before calling the $(document).ready()etc. functions. However, they were all in the correct positions.

看看这里的答案,很明显我在调用$(document).ready()等函数之前没有加载我的 jquery 文件。然而,他们都处于正确的位置。

In my case, this was because I was accessing the content over a secure HTTPS connection, whereas the page was trying to download the CDN hosted data from google, etc. The solution was to store them locally and then include then directly rather than from the CDN each time.

就我而言,这是因为我通过安全的 HTTPS 连接访问内容,而页面试图从谷歌等下载 CDN 托管数据。解决方案是将它们存储在本地,然后直接包含而不是从每次都CDN。

Edit: The other way of doing this is to link to all the CDN-hosted stuff as https:// rather than http:// - then the model doesn't complain.

编辑:另一种方法是将所有 CDN 托管的内容链接为 https:// 而不是 http:// - 然后模型不会抱怨。

回答by lalithkumar

Add the library before you start the script.You can add any of these following CDNto start it.

在启动脚本之前添加库。您可以添加以下CDN 中的任何一个来启动它。

Google:

谷歌:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>

Microsoft

微软

<script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.2.1.min.js"></script>

Jquery

查询

If you want any other Jquery cdn version check this link.

如果您想要任何其他 Jquery CDN 版本,请检查此链接

After this:

在这之后:

<script type="text/javascript">
$(function(){
    //your stuff
});
or
$(document).ready(function(){
    //your stuff
});    
</script>

Wordpress:

WordPress:

<script type="text/javascript">
var $ = jQuery;
jQuery(document).ready(function($){
     //your stuff
});
</script>

回答by xoxn-- 1'w3k4n

If it's in wordpress, might be needed to change

如果它在 wordpress 中,可能需要更改

$(document).ready(function() {

to

jQuery(document).ready(function($){

or add

或添加

var $ = jQuery;

before

$(document).ready(function() {

回答by crowicked

I had the exact same problem and none of these solutions above helped. however, I just linked the .cssfiles after the .jsfiles and the problem miraculously disappeared. Hope this helps.

我遇到了完全相同的问题,上面的这些解决方案都没有帮助。但是,我只是在.css文件后链接.js文件,问题奇迹般地消失了。希望这可以帮助。

回答by Yannis Dran

In my case I had this referenceError as the order of script calls was wrong. Solved that by changing the order:

在我的情况下,我有这个 referenceError 因为脚本调用的顺序是错误的。通过更改顺序解决了这个问题:

<script src="js/index.js"></script>
<script src="js/jquery-1.10.2.js"></script>

to

<script src="js/jquery-1.10.2.js"></script>
<script src="js/index.js"></script>