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:04

I have also faced such problems many time in web develoment carrier. Actually its not JS conflict, When we load html website to the browser we face no such error, but when we load these type of website through localhost we face such problem. That's because of localhost. When we load scripts through the localhost it scans the script and renders the output. But when we didn't use localhost. It just scan the output. Example, when we write php code putside the localhost and run without host we get error. But if the code is correct and is run through host we get actual output, and when we use inspect element we get the output code in HTMl format but not in PHP format this is because of rendering of the code.

This is rendering error. So to fix these jquery code error one of the solution may be using this method.

jQuery(document).ready(function($){
/******** Body of Jquery Code*****/
});

What this code does is register '$' as the varible to the function by applying jquery. Else by default the .js file is only read as javascript.

查看更多
弹指情弦暗扣
3楼-- · 2018-12-31 09:05

Try this snippet:

jQuery(function($) {
  // Your code.
})

It worked for me, maybe it will help you too.

查看更多
十年一品温如言
4楼-- · 2018-12-31 09:08

You can pass $ in function()

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

// jQuery code is in here

});

or you can replace $(document); with this jQuery(document);

or you can use jQuery.noConflict()

var jq=jQuery.noConflict();
jq(document).ready(function(){  
  jq('selector').show();
});
查看更多
琉璃瓶的回忆
5楼-- · 2018-12-31 09:09

I would use this

(function ($) {
   $(document);
}(jQuery));
查看更多
梦醉为红颜
6楼-- · 2018-12-31 09:13

Use jQuery's noConflict. It did wonders for me

var example=jQuery.noConflict();
example(function(){
example('div#rift_connect').click(function(){
    example('span#resultado').text("Hello, dude!");
    });
});

That is, assuming you included jQuery on your HTML

<script language="javascript" type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
查看更多
姐姐魅力值爆表
7楼-- · 2018-12-31 09:14

I ran into this problem also when including jQuery in my page header, not realizing the host was already including it in the page automatically. So load your page live and check the source to see if jQuery is being linked in.

查看更多
登录 后发表回答