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

May 16, 2016 pm 05:04 PM
function jquery

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.

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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Detailed explanation of jQuery reference methods: Quick start guide Detailed explanation of jQuery reference methods: Quick start guide Feb 27, 2024 pm 06:45 PM

Detailed explanation of jQuery reference methods: Quick start guide

How to use PUT request method in jQuery? How to use PUT request method in jQuery? Feb 28, 2024 pm 03:12 PM

How to use PUT request method in jQuery?

How to remove the height attribute of an element with jQuery? How to remove the height attribute of an element with jQuery? Feb 28, 2024 am 08:39 AM

How to remove the height attribute of an element with jQuery?

jQuery Tips: Quickly modify the text of all a tags on the page jQuery Tips: Quickly modify the text of all a tags on the page Feb 28, 2024 pm 09:06 PM

jQuery Tips: Quickly modify the text of all a tags on the page

Use jQuery to modify the text content of all a tags Use jQuery to modify the text content of all a tags Feb 28, 2024 pm 05:42 PM

Use jQuery to modify the text content of all a tags

Detailed explanation of the role and function of the MySQL.proc table Detailed explanation of the role and function of the MySQL.proc table Mar 16, 2024 am 09:03 AM

Detailed explanation of the role and function of the MySQL.proc table

Understand the role and application scenarios of eq in jQuery Understand the role and application scenarios of eq in jQuery Feb 28, 2024 pm 01:15 PM

Understand the role and application scenarios of eq in jQuery

How to tell if a jQuery element has a specific attribute? How to tell if a jQuery element has a specific attribute? Feb 29, 2024 am 09:03 AM

How to tell if a jQuery element has a specific attribute?

See all articles