TypeError: $ is not a function when calling jQuery function

后端 未结 16 1844
慢半拍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:47

    What worked for me. The first library to import is the query library and right then call the jQuery.noConflict() method.

    <head>
     <script type="text/javascript" src="jquery.min.js"/>
     <script>
      var jq = jQuery.noConflict();
      jq(document).ready(function(){
      //.... your code here
        });
     </script>
    
    0 讨论(0)
  • 2020-11-22 02:49

    By default when you enqueue jQuery in Wordpress you must use jQuery, and $ is not used (this is for compatibility with other libraries).

    Your solution of wrapping it in function will work fine, or you can load jQuery some other way (but that's probably not a good idea in Wordpress).

    If you must use document.ready, you can actually pass $ into the function call:

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

    This solution worked for me

    ;(function($){
        // your code
    })(jQuery);
    

    Move your code inside the closure and use $ instead of jQuery

    I found the above solution in https://magento.stackexchange.com/questions/33348/uncaught-typeerror-undefined-is-not-a-function-when-using-a-jquery-plugin-in-ma

    ...after searching too much

    0 讨论(0)
  • 2020-11-22 02:51
    <script>
    var jq=jQuery.noConflict();
    (function ($) 
        {
        function nameoffunction()
        {
            // Set your code here!!
        }
    
        $(document).ready(readyFn); 
        })(jQuery);
    

    now use jq in place of jQuery

    0 讨论(0)
  • 2020-11-22 02:55

    Also, I find the good solution for use jQuery noConflict mode.

    (function($){
    
      $(document).ready(function(){
          // write code here
      });
    
      // or also you can write jquery code like this
    
      jQuery(document).ready(function(){
          // write code here
      });
    
    })(jQuery);
    

    I found this solution from here. https://thecodingstuff.com/how-to-properly-use-jquery-noconflict-mode-in-wordpress/

    0 讨论(0)
  • 2020-11-22 02:56
    var $=jQuery.noConflict();
    
    $(document).ready(function(){
        // jQuery code is in here
    });
    

    Credit to Ashwani Panwar and Cyssoo answer: https://stackoverflow.com/a/29341144/3010027

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