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 08:54

Also check for including jQuery, followed by some components/other libraries (like jQuery UI) and then accidentially including jQuery again - this will redefine jQuery and drop the component helpers (like .datepicker) off the instance.

查看更多
只若初见
3楼-- · 2018-12-31 08:59

I had this problem only on Chrome.

I tried adding

var $ =jQuery.noConflict();

just before calling

$(document).ready(function () {

It worked.

Thanks a lot

查看更多
不流泪的眼
4楼-- · 2018-12-31 09:03

Use this:

var $ =jQuery.noConflict();
查看更多
怪性笑人.
5楼-- · 2018-12-31 09:03
 ;(function($){
        // your code
    })(jQuery);

Place your js code inside the closure above , it should solve the problem.

查看更多
无与为乐者.
6楼-- · 2018-12-31 09:03

Two things:

  1. Be sure that you have jQuery library added, before your $(document).
  2. Then just change all "$" with: jQuery , as the previous comments.
查看更多
几人难应
7楼-- · 2018-12-31 09:03

wrap all the script between this...

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

Many JavaScript libraries use $ as a function or variable name, just as jQuery does. In jQuery's case, $ is just an alias for jQuery, so all functionality is available without using $. If you need to use another JavaScript library alongside jQuery, return control of $ back to the other library with a call to $.noConflict(). Old references of $ are saved during jQuery initialization; noConflict() simply restores them.

查看更多
登录 后发表回答