jQuery fix for “Uncaught TypeError: $ is not a fun

2019-02-05 04:33发布

问题:

This question already has an answer here:

  • TypeError: $ is not a function when calling jQuery function 15 answers

My code:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>      
<script src="http://someothersite.com/external.js"></script>

external.js:

$("head").append(unescape(""));

Unfortunately I am getting the following error when I include my external script:

Uncaught TypeError: $ is not a function

How do I fix this? Please bear in mind I can't edit the external Javascript file as it's third party.

回答1:

Use the following statement in a JS file.

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

// jQuery code is in here

});

After declaring the above statement you will be able to use $ sign.



回答2:

It was using an old version of jQuery.

I updated the version and this resolved the issue for me.

Woop!



回答3:

I think the issue was because the external javascript was loaded first before the jquery was loaded. To solve this use requirejs, follow this link for the usage http://requirejs.org/docs/api.html