Home >Web Front-end >JS Tutorial >jquery method of loading a page (executed after the page is loaded)_jquery

jquery method of loading a page (executed after the page is loaded)_jquery

WBOY
WBOYOriginal
2016-05-16 18:05:391036browse

1. $(function(){
 $("#a").click(function(){
   //adding your code here
 });
});
2 , $(document).ready(function(){
 $("#a").click(function(){
   //adding your code here  
 });
});
3. window.onload = function(){
 $("#a").click(function(){
   //adding your code here
 });
}
The html code is Click , and the page needs to reference the js file of jquery

The general method of calling js when loading a page is as follows:

window.onload = function() {
$("table tr:nth-child(even)").addClass("even"); //This is jquery code
};

This code will be displayed on the entire page Executed after all documents are loaded. Unfortunately, this method not only requires that the DOM tree of the page be fully loaded, but also requires that all external images and resources be loaded. What's even more unfortunate is that if external resources, such as images, take a long time to load, then this js effect will make the user feel ineffective.

But use the jquery method:

$(document).ready(function() {

// Any js special effects that need to be executed
$("table tr:nth-child(even)").addClass("even");
});

just needs to load all the DOM structures and put all the HTML into the DOM in the browser The js effect is executed before tree. Included before loading external images and resources.

There is also a shorthand way:

$(function() {

// Any js special effects that need to be executed
$("table tr:nth- child(even)").addClass("even");
});

Statement:
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