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

前端 未结 14 1857
猫巷女王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:32
     ;(function($){
            // your code
        })(jQuery);
    

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

    0 讨论(0)
  • 2020-11-22 07:32

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

    jQuery( document ).ready(function( $ ) {
      // jQuery(document)
      // $(document)
    });
    
    0 讨论(0)
  • 2020-11-22 07:37

    Use this:

    var $ =jQuery.noConflict();
    
    0 讨论(0)
  • 2020-11-22 07:41

    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.

    0 讨论(0)
  • 2020-11-22 07:42

    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();
    });
    
    0 讨论(0)
  • 2020-11-22 07:43

    I had this problem only on Chrome.

    I tried adding

    var $ =jQuery.noConflict();
    

    just before calling

    $(document).ready(function () {
    

    It worked.

    Thanks a lot

    0 讨论(0)
提交回复
热议问题