TypeError: $ is not a function when calling jQuery function

后端 未结 16 1847
慢半拍i
慢半拍i 2020-11-22 02:42

I have a simple jQuery script in a WordPress plugin that is using a jQuery wrapper like this:

$(document).ready(function(){

    // jQuery code is in here

}         


        
相关标签:
16条回答
  • 2020-11-22 02:57

    replace $ sign with jQuery like this:

    jQuery(function(){
    //your code here
    });
    
    0 讨论(0)
  • 2020-11-22 02:57

    You can use

    jQuery(document).ready(function(){ ...... });
    

    or

    (function ($) { ...... }(jQuery));
    
    0 讨论(0)
  • 2020-11-22 03:02

    You have to pass $ in function()

    <script>
    jQuery(document).ready(function($){
    
    // jQuery code is in here
    
    });
    </script>
    
    0 讨论(0)
  • 2020-11-22 03:04

    You come across this issue when your function name and one of the id names in the file are same. just make sure all your id names in the file are unique.

    0 讨论(0)
  • 2020-11-22 03:08

    This should fix it:

    jQuery(document).ready(function($){
      //you can now use $ as your jQuery object.
      var body = $( 'body' );
    });
    

    Put simply, WordPress runs their own scripting before you can and they release the $ var so it won't collide with other libraries. This makes total sense, as WordPress is used for all kinds of web sites, apps, and of course, blogs.

    From their documentation:

    The jQuery library included with WordPress is set to the noConflict() mode (see wp-includes/js/jquery/jquery.js). This is to prevent compatibility problems with other JavaScript libraries that WordPress can link.

    In the noConflict() mode, the global $ shortcut for jQuery is not available...

    0 讨论(0)
  • 2020-11-22 03:08

    Try this:

    <script language="JavaScript" type="text/javascript" src="jquery/jquery.js"></script>
    <script>
        jQuery.noConflict();
        (function ($) {
            function readyFn() {
                // Set your code here!!
            }
    
            $(document).ready(readyFn); 
        })(jQuery);
    
    </script>
    
    0 讨论(0)
提交回复
热议问题