There are two types of events: ready, which means that the document structure has been loaded (not including images or other non-text media), and onload, Indicates that all elements of the page, including files such as images, have loaded.

$(function(){// do something}); $(document). Ready (function(){//do something}) // The default parameter of jQuer is document; $().ready(function(){//do something}) jq ready() {//do something}) Generally, the first page response loading sequence is: domain name resolution – loading HTML – loading JS and CSS – loading images and other information. Dom Ready should be Ready to manipulate the Dom between loading JS and CSS and loading images and other information.

1. Windows. Onload method

(1) Execution time: execute after all elements in the web page (including all associated files of elements) are fully loaded into the browser, that is, JavaScript can access all elements in the web page at this time. Window. &western nl &western AD = function () {$(window). The load (the function () {/ / write code is equivalent to / / write code}});

(2) Multiple uses: JavaScript’s onload event can only hold a reference to one function at a time, and it automatically overwrites the previous function with the last one. function one(){

Alert (” one “);

}

function two(){

Alert (” two “);

}

Window. &western nl &western AD = one;

Window. &western nl &western AD = two;

// There are only two after running the code

2. The $(document). Ready () method

(1) Execution timing: It can be called when the DOM is fully ready. (This does not mean that all the files associated with these elements have been downloaded.) For example, the $(document).ready() method knows that the DOM is ready, rather than waiting for all images to download.

Function one(){function one(){

Alert (” one “);

}

function two(){

Alert (” two “);

}

$(document).ready(function(){

one();

});

$(document).ready(function(){

two();

});

// After running the code

// First: one

// First: two

The original link