TypeError: $ is not a function when calling jQuery function

后端 未结 16 2024
慢半拍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 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...

提交回复
热议问题