Difference between jQuery(function(){}) and (function(){})(jQuery) in jQuery

1. jQuery(function(){ });
is used to store the code for operating the DOM object, and the DOM object already exists when the code is executed. It cannot be used to store the code for developing plug-ins, because the jQuery object is not passed, and the methods (functions) in it cannot be called externally through jQuery.method.
2. (function(){ })(jQuery);
It is used to store the code for developing the plug-in. The DOM does not necessarily exist when the code is executed, so please use the code that directly executes the DOM operation with care.
The format for developing a plugin is as follows:
    (function ($) {
        $.fn.test = function () {
            alert('test')
        }
    })(jQuery);

call the plugin
$("#elementid").test ();
Note :
(function($){...})(jQuery) is actually an anonymous function

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326114077&siteId=291194637