$ (function () {}) and $ (document). Use of Ready (function () {})

Source: Internet
Author: User

When the document is loaded and executed, the following effects are equivalent:
1.
$ (function() {  // This is the shorthand for jquery ready (), that is, the next 2 shorthand   // do something });

2.

$ (document). Ready (function() {//dosomething})

3.

$ (). Ready (function() {  /// default parameter is: Document//do Something})

the difference between Document.ready and onload--javascript document load completion eventThere are two kinds of events for page load completion:One is ready, indicating that the document structure is loaded (not including non-text media files such as pictures)The second is onload, which indicates that all elements, including pictures and other files, are loaded and completed. 1. $ (document). Ready () method (1) Execution time: it can be called when the DOM is fully ready. (This does not mean that the files associated with these elements have already been downloaded)For example: $ (document). The Ready () method can be manipulated knowing that the DOM is in place, without waiting for all pictures to be downloaded.(2) Multiple use:
function One () {   alert ("one");} function Both () {   alert ("both");} $ (document). Ready (function() {one  ();});
$ (document). Ready (function() {n ();}); // after running the code // First: one // First:

2.. Window.onload Method (1) Execution time: All elements in the Web page, including all associated files of the element, are fully loaded into the browser before they are executed, that is, JavaScript can now access all the elements in the Web page. Window.onload=function () {$ (window). Load (function () {Writing codeequivalent to //write code }  });(2) Multiple use:the onload event of JavaScript can only hold a reference to a function at a time , and he will automatically overwrite the previous function with the last function.
function One () {   alert ("one");} function Both () {   alert ("both");} Window.onload=one;window.onload= both; // only one after running the code

$ (function () {}) and $ (document). Use of Ready (function () {})

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.