TypeError: 'undefined' is not a function (

2018-12-31 08:44发布

  1. I'm using a WordPress site.
  2. I'm including this script in the header.

When the script loads, I get this error:

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

I have no idea what is causing it or what it even means.

In firebug, I get this:

$ is not a function

14条回答
旧时光的记忆
2楼-- · 2018-12-31 09:14

You can use both jQuery and $ in below snippet. it worked for me

jQuery( document ).ready(function( $ ) {
  // jQuery(document)
  // $(document)
});
查看更多
伤终究还是伤i
3楼-- · 2018-12-31 09:17

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));
查看更多
登录 后发表回答