Wordpress 如何使用 jquery 和 $ 符号 [英] Wordpress how to use jquery and $ sign

查看:23
本文介绍了Wordpress 如何使用 jquery 和 $ 符号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用 jQuery 包装器的 WordPress 插件中有一个简单的 jQuery 脚本,如下所示:

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

});

我从 WordPress 仪表板中调用此脚本,并在 jQuery 框架加载后加载它.

I am calling this script from within the WordPress Dashboard and am loading it AFTER the jQuery framework has loaded.

当我在 Firebug 中检查页面时,我不断收到错误消息:

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

类型错误:$ 不是函数

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.

任何帮助将不胜感激.

推荐答案

默认情况下,当您在 Wordpress 中排队 jQuery 时,您必须使用 jQuery,并且不使用 $ (这是为了与其他库兼容).

By default when you enqueue jQuery in Wordpress you must use jQuery, and $ is not used (this is for compatibility with other libraries).

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

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

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

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

jQuery(function ($) { ...

这篇关于Wordpress 如何使用 jquery 和 $ 符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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