Javascript jQuery 修复“Uncaught TypeError: $ is not a function”错误

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

jQuery fix for "Uncaught TypeError: $ is not a function" error

javascriptjquerywordpress

提问by michaelmcgurk

My code:

我的代码:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>      
<script src="http://someothersite.com/external.js"></script>

external.js:

外部.js:

$("head").append(unescape(""));

Unfortunately I am getting the following error when I include my external script:

不幸的是,当我包含外部脚本时出现以下错误:

Uncaught TypeError: $ is not a function

未捕获的类型错误:$ 不是函数

How do I fix this? Please bear in mind I can't edit the external Javascript file as it's third party.

我该如何解决?请记住,我无法编辑外部 Javascript 文件,因为它是第三方的。

采纳答案by michaelmcgurk

It was using an old version of jQuery.

它使用的是旧版本的 jQuery。

I updated the version and this resolved the issue for me.

我更新了版本,这为我解决了问题。

Woop!

哇!

回答by WisdmLabs

Use the following statement in a JS file.

在 JS 文件中使用以下语句。

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

// jQuery code is in here

});

After declaring the above statement you will be able to use $ sign.

声明上述语句后,您将能够使用 $ 符号。

回答by Neil Villareal

I think the issue was because the external javascript was loaded first before the jquery was loaded. To solve this use requirejs, follow this link for the usage http://requirejs.org/docs/api.html

我认为问题是因为在加载 jquery 之前首先加载了外部 javascript。要解决这个使用requirejs,按照这个链接使用http://requirejs.org/docs/api.html