TypeError:$在调用jQuery函数时不是函数 [英] TypeError: $ is not a function when calling jQuery function

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

问题描述

  $(document)在一个WordPress插件中有一个简单的jQuery脚本,它使用如下的jQuery包装: .ready(function(){

// jQuery代码在这里

});

我在WordPress Dashboard中调用此脚本,并在加载jQuery框架后加载它。



当我在Firebug中检查页面时,我总是收到错误信息:


TypeError:$不是函数

$(document).ready(function(){


我应该用这个函数包装脚本:

 (function($){

// jQuery代码在这里
$ b $)(jQuery);

我有这个错误不止一次,不知道如何处理它。



任何帮助都将不胜感激。

解决方案

默认情况下,当您在Wordpress中入队jQuery时,您必须使用 jQuery 和<$不使用c $ c> $ (这是为了与其他库兼容)。



将你的解决方案包装在福nction 可以正常工作,或者你可以用其他方式加载jQuery(但这在Wordpress中可能不是个好主意)。



如果你必须使用 document.ready ,实际上可以将 $ 传递给函数调用:

  jQuery(function($){... 


I have a simple jQuery script in a WordPress plugin that is using a jQuery wrapper like this:

$(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.

When I check the page in Firebug I constantly keep receiving the error message:

TypeError: $ is not a 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 default when you enqueue jQuery in Wordpress you must use jQuery, and $ is not used (this is for compatibility with other libraries).

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).

If you must use document.ready, you can actually pass $ into the function call:

jQuery(function ($) { ...

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

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