Home > Web Front-end > JS Tutorial > A brief analysis of the difference between jQuery(function(){}) and (function(){})(jQuery)_jquery

A brief analysis of the difference between jQuery(function(){}) and (function(){})(jQuery)_jquery

WBOY
Release: 2016-05-16 17:04:40
Original
981 people have browsed it

Jquery is an excellent Javascrīpt framework. Let’s now discuss the functions that are executed after two pages are loaded in Jquery.

$(document).ready(function(){
// Write your code here...
}); Code that runs when the DOM is loaded

can be abbreviated as

jQuery(function(){
});

(function($) {})(jQuery) What does it mean?
(function(){
})(jQuery);

It actually executes the ()(para) anonymous method, but just passes the jQuery object.

Equivalent to
function aa($){}
aa(jQuery)

is the idiomatic way to initialize jquery objects.

In layman's terms, it means executing the code you need after the page is loaded.
However, this thing sometimes makes the page jump. Many JQUERY plug-ins only change the style after the page is loaded, and the page will There is a feeling of jumping or flickering. For example, with the ui.tab plug-in, when there are many page elements, they are all displayed and it forms a TAB. It’s very confusing

(funtion(){})(); execute the function immediately; equivalent to declaring a function first and calling it directly after the declaration;

If the parameters are like:
(funtion(str){alert(str)})("output")); equivalent to: funtion OutPutFun(str){alert(str);};OutPutFun("output" );

jQuery(function(){}); is used to store code that operates DOM objects. The DOM object already exists when the code is executed. It cannot be used to store code for developing plug-ins, because the jQuery object is not passed, and its methods (functions) cannot be called externally through jQuery.method.
(function(){})(jQuery); is used to store the code for developing plug-ins. The DOM may not exist when the code is executed, so please use the code that directly automatically performs DOM operations with caution.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template