TypeError: 'undefined' is not a function (evaluating '$(document)')

前端 未结 14 1889
猫巷女王i
猫巷女王i 2020-11-22 07:13
  1. I\'m using a WordPress site.
  2. I\'m including this script in the header.

When the script loads, I get this error:

TypeErro

14条回答
  •  逝去的感伤
    2020-11-22 07:49

    Wordpress uses jQuery in noConflict mode by default. You need to reference it using jQuery as the variable name, not $, e.g. use

    jQuery(document);
    

    instead of

    $(document);
    

    You can easily wrap this up in a self executing function so that $ refers to jQuery again (and avoids polluting the global namespace as well), e.g.

    (function ($) {
       $(document);
    }(jQuery));
    

提交回复
热议问题