类型错误:调用 jQuery 函数时 $ 不是函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12343714/
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
TypeError: $ is not a function when calling jQuery function
提问by Jason
I have a simple jQuery script in a WordPress plugin that is using a jQuery wrapper like this:
我在使用 jQuery 包装器的 WordPress 插件中有一个简单的 jQuery 脚本,如下所示:
$(document).ready(function(){
// jQuery code is in here
});
I am calling this script from within the WordPress Dashboard and am loading it AFTER the jQuery framework has loaded.
我正在 WordPress 仪表板中调用此脚本,并在 jQuery 框架加载后加载它。
When I check the page in Firebug I constantly keep receiving the error message:
当我在 Firebug 中检查页面时,我不断收到错误消息:
TypeError: $ is not a function
$(document).ready(function(){
类型错误:$ 不是函数
$(document).ready(function(){
Should I maybe wrap the script in this function:
我是否应该将脚本包装在这个函数中:
(function($){
// jQuery code is in here
})(jQuery);
I have had this error quite a few times and am not sure how to handle it.
我已经多次遇到此错误,但不确定如何处理。
Any help would be greatly appreciated.
任何帮助将不胜感激。
回答by Explosion Pills
By default when you enqueue jQuery in Wordpress you must use jQuery
, and $
is not used (this is for compatibility with other libraries).
默认情况下,当您在 Wordpress 中排队 jQuery 时,您必须使用jQuery
, 并且$
不使用(这是为了与其他库兼容)。
Your solution of wrapping it in function
will work fine, or you can load jQuery some other way (but that's probably not a good idea in Wordpress).
您将其包装起来的解决方案function
可以正常工作,或者您可以以其他方式加载 jQuery(但这在 Wordpress 中可能不是一个好主意)。
If you must use document.ready
, you can actually pass $
into the function call:
如果你必须使用document.ready
,你实际上可以传入$
函数调用:
jQuery(function ($) { ...
回答by Matthew Blancarte
This should fix it:
这应该解决它:
jQuery(document).ready(function($){
//you can now use $ as your jQuery object.
var body = $( 'body' );
});
Put simply, WordPress runs their own scripting before you can and they release the $
var so it won't collide with other libraries. This makes total sense, as WordPress is used for all kinds of web sites, apps, and of course, blogs.
简而言之,WordPress 在您可以之前运行自己的脚本,并且他们会释放$
var,这样它就不会与其他库发生冲突。这是完全有道理的,因为 WordPress 用于各种网站、应用程序,当然还有博客。
From their documentation:
从他们的文档:
The jQuery library included with WordPress is set to the noConflict() mode (see wp-includes/js/jquery/jquery.js). This is to prevent compatibility problems with other JavaScript libraries that WordPress can link.
In the noConflict() mode, the global $ shortcut for jQuery is not available...
WordPress 附带的 jQuery 库设置为 noConflict() 模式(请参阅 wp-includes/js/jquery/jquery.js)。这是为了防止与 WordPress 可以链接的其他 JavaScript 库出现兼容性问题。
在 noConflict() 模式下,jQuery 的全局 $ 快捷方式不可用...
回答by Bikram Shrestha
This solution worked for me
这个解决方案对我有用
;(function($){
// your code
})(jQuery);
Move your code inside the closure and use $
instead of jQuery
将您的代码移到闭包内并使用$
而不是jQuery
I found the above solution in https://magento.stackexchange.com/questions/33348/uncaught-typeerror-undefined-is-not-a-function-when-using-a-jquery-plugin-in-ma
我在https://magento.stackexchange.com/questions/33348/uncaught-typeerror-undefined-is-not-a-function-when-using-a-jquery-plugin-in-ma 中找到了上述解决方案
...after searching too much
...搜索太多之后
回答by Ashwani Panwar
You can avoid confliction like this
你可以避免这样的冲突
var jq=jQuery.noConflict();
jq(document).ready(function(){
alert("Hi this will not conflict now");
jq('selector').show();
});
回答by optimiertes
var $=jQuery.noConflict();
$(document).ready(function(){
// jQuery code is in here
});
Credit to Ashwani Panwar and Cyssoo answer: https://stackoverflow.com/a/29341144/3010027
感谢 Ashwani Panwar 和 Cyssoo 的回答:https://stackoverflow.com/a/29341144/3010027
回答by Jose Carlos Ramos Carmenates
Try this:
尝试这个:
<script language="JavaScript" type="text/javascript" src="jquery/jquery.js"></script>
<script>
jQuery.noConflict();
(function ($) {
function readyFn() {
// Set your code here!!
}
$(document).ready(readyFn);
})(jQuery);
</script>
回答by Jitendra Damor
You have to pass $ in function()
您必须在 function() 中传递 $
<script>
jQuery(document).ready(function($){
// jQuery code is in here
});
</script>
回答by MaXee Khan
replace $
sign with jQuery
like this:
$
用jQuery
这样的替换符号:
jQuery(function(){
//your code here
});
回答by Raghul Manoharan
Double check your jQuery references. It is possible that you are either referencing it more than once or you are calling your function too early (before jQuery is defined). You can try as mentioned in my comments and put any jQuery reference at the top of your file (in the head) and see if that helps.
仔细检查您的 jQuery 引用。您可能会多次引用它,或者您过早地调用您的函数(在定义 jQuery 之前)。您可以按照我的评论中提到的方法进行尝试,并将任何 jQuery 引用放在文件顶部(在头部),看看是否有帮助。
If you use the encapsulation of jQuery it shouldn't help in this case. Please try it because I think it is prettier and more obvious, but if jQuery is not defined you will get the same errors.
如果您使用 jQuery 的封装,则在这种情况下应该无济于事。请尝试一下,因为我认为它更漂亮,更明显,但是如果未定义 jQuery,您将收到相同的错误。
In the end... jQuery is not currently defined.
最后...... jQuery 目前没有定义。
回答by Renish Khunt
Also, I find the good solution for use jQuery noConflict mode.
另外,我找到了使用 jQuery noConflict 模式的好解决方案。
(function($){
$(document).ready(function(){
// write code here
});
// or also you can write jquery code like this
jQuery(document).ready(function(){
// write code here
});
})(jQuery);
I found this solution from here. https://thecodingstuff.com/how-to-properly-use-jquery-noconflict-mode-in-wordpress/
我从这里找到了这个解决方案。 https://thecodingstuff.com/how-to-properly-use-jquery-noconflict-mode-in-wordpress/