Javascript 类型错误:$ 不是 WordPress 函数

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

TypeError: $ is not a function WordPress

javascriptjquerywordpress

提问by user1645326

I might have an error in a .jsdocument in a theme I have purchase:

.js购买的主题中的文档可能有错误:

$('.tagcloud a').wrap('<span class="st_tag" />');

I am trying to solve it but I am not a programmer so I don't know how. The site is this: http://www.framerental.com.

我正在尝试解决它,但我不是程序员,所以我不知道如何解决。该网站是这样的:http: //www.framerental.com

回答by jurka

You use jQuery.noConflict();So $is undefined.

您使用jQuery.noConflict();So$是未定义的。

You can read more about it here docs

你可以在这里阅读更多关于它的文档

Try to modify your code in this way (add $sign to ready function):

尝试以这种方式修改您的代码($向就绪函数添加符号):

jQuery(document).ready(function($) {
    // Code that uses jQuery's $ can follow here.
});

回答by Simon Hayter

Function errors are a common thing in almost all content management systems and there is a few ways you can approach this.

功能错误在几乎所有内容管理系统中都很常见,您可以通过几种方法来解决这个问题。

  1. Wrap your code using:

    <script> 
    jQuery(function($) {
    
    YOUR CODE GOES HERE
    
    });
    </script>
    
  2. You can also use jQuery's API using noConflict();

    <script>
    $.noConflict();
    jQuery( document ).ready(function( $ ) {
    // Code that uses jQuery's $ can follow here.
    });
    // Code that uses other library's $ can follow here.
    </script>
    
  3. Another example of using noConflict without using document ready:

    <script>
    jQuery.noConflict();
        (function( $ ) {
            $(function() { 
                // YOUR CODE HERE
            });
         });
    </script>
    
  4. You could even choose to create your very alias to avoid conflicts like so:

    var jExample = jQuery.noConflict();
    // Do something with jQuery
    jExample( "div p" ).hide();
    
  5. Yet another longer solution is to rename all referances of $ to jQuery:

    $( "div p" ).hide();to jQuery( "div p" ).hide();

  1. 使用以下方法包装您的代码:

    <script> 
    jQuery(function($) {
    
    YOUR CODE GOES HERE
    
    });
    </script>
    
  2. 您还可以使用 jQuery 的 API noConflict();

    <script>
    $.noConflict();
    jQuery( document ).ready(function( $ ) {
    // Code that uses jQuery's $ can follow here.
    });
    // Code that uses other library's $ can follow here.
    </script>
    
  3. 另一个使用 noConflict 而不使用 document ready 的例子:

    <script>
    jQuery.noConflict();
        (function( $ ) {
            $(function() { 
                // YOUR CODE HERE
            });
         });
    </script>
    
  4. 您甚至可以选择创建自己的别名以避免冲突,如下所示:

    var jExample = jQuery.noConflict();
    // Do something with jQuery
    jExample( "div p" ).hide();
    
  5. 另一个更长的解决方案是将 $ 的所有引用重命名为 jQuery:

    $( "div p" ).hide();jQuery( "div p" ).hide();

回答by Guy Castell

Instead of doing this:

而不是这样做:

$(document).ready(function() { });

You should be doing this:

你应该这样做:

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

    // your code goes here

});

This is because WordPress may use $ for something other than jQuery, in the future, or now, and so you need to load jQuery in a way that the $ can be used only in a jQuery document ready callback.

这是因为 WordPress 可能会在未来或现在将 $ 用于 jQuery 以外的其他东西,因此您需要以一种方式加载 jQuery,即 $ 只能在 jQuery 文档就绪回调中使用。

回答by Fanky

Just add this:

只需添加这个:

<script>
var $ = jQuery.noConflict();
</script>

to the head tag in header.php . Or in case you want to use the dollar sign in admin area (or somewhere, where header.php is not used), right before the place you want to use the it.

到 header.php 中的 head 标签。或者,如果您想在管理区域(或未使用 header.php 的某处)中使用美元符号,就在您想使用它的地方之前。

(There might be some conflicts that I'm not aware of, test it and if there are, use the other solutions offered here or at the link bellow.)

(可能存在一些我不知道的冲突,请对其进行测试,如果有,请使用此处或以下链接中提供的其他解决方案。)

Source: http://www.wpdevsolutions.com/use-the-dollar-sign-in-wordpress-instead-of-jquery/

来源:http: //www.wpdevsolutions.com/use-the-dollar-sign-in-wordpress-instead-of-jquery/

回答by Elias Van Ootegem

Either you're not including jquery toolkit/lib, as some have suggested, or there is a conflict of sorts. To test: include jQuery and test like this:

要么你不包括 jquery 工具包/lib,正如一些人所建议的那样,要么存在各种冲突。测试:包含 jQuery 并像这样测试:

console.log($);
console.log($ === jQuery);

If $is not undefined, and $ === jQuerylogs false, you definitely have a conflict on your hands. Replacing your $with jQuerysolves that, but that can be quite tedious (all that extra typing...). Generally I start my scripts with $jq = _$ = jQuery;to at least have a shorter reference to the jQuery object.
Of course, before you do that, check to see if you're not accidentally overriding variables that have been set beforehand: console.log($jq, _jQ, _$);whichever is notundefinedshould be left alone, of course

如果$不是 undefined,并且$ === jQuery记录为 false,那么您肯定会遇到冲突。用$with替换可以jQuery解决这个问题,但这可能非常乏味(所有额外的输入......)。通常,我开始我的脚本时$jq = _$ = jQuery;,至少有一个对 jQuery 对象的较短引用。
当然,在你这样做之前,检查一下你是否不小心覆盖了事先设置的变量:当然,console.log($jq, _jQ, _$);不管哪个都不undefined应该被搁置

回答by Savage

I added a custom script file that loaded at the end of the headsection, and inserted the following at the top:

我添加了一个在本head节末尾加载的自定义脚本文件,并在顶部插入了以下内容:

(function (jQuery) {
    window.$ = jQuery.noConflict();
})(jQuery);

(This is a modification of Fanky's answer)

(这是对Fanky答案的修改)

回答by Waqar Alamgir

Other than noConflict, this is helpful too:

除了 noConflict,这也很有帮助:

(function( $ ) {
    // Variables and DOM Caching using $.
    var body = $('body');
    console.log(body);
})( jQuery );

回答by Giles Tamplin

If you have included jQuery, there may be a conflict. Try using jQueryinstead of $.

如果您已包含 jQuery,则可能存在冲突。尝试使用jQuery代替$.

回答by Michael

There is an easy way to fix it. Just install a pluginand active it. For more : https://www.aitlp.com/wordpress-5-3-is-not-a-function-fix/.

有一种简单的方法可以修复它。只需安装一个插件并激活它。更多信息:https: //www.aitlp.com/wordpress-5-3-is-not-a-function-fix/

Tested with Wordpress version 5.3.

使用 Wordpress 5.3 版测试。

回答by Dan Osborne

I was able to resolve this very easily my simply enqueuing jQuery wp_enqueue_script("jquery");

我能够很容易地解决这个问题,我只是简单地将 jQuery wp_enqueue_script("jquery");