Call JS Implementation when loading pages using jquery _jquery

Source: Internet
Author: User

1,window.onload = function () {};

2,$ (document). Ready (function () {});

One, the general load page when calling the JS method is as follows:

Window.onload = function () {  
$ ("Table tr:nth-child (even)"). AddClass ("even");//This is the jquery code  
}; 

This code is executed after the entire page's document is loaded and completed. Unfortunately this way not only requires that the DOM tree of the page be loaded completely, but all external pictures and resources are required to complete loading. Unfortunately, if the external resources, such as the picture takes a long time to load, then this JS effect will make the user feel ineffective.

Second, using jquery method to load the page

$ (document). Ready (function () {  
//any need to perform JS effect  
$ ("Table tr:nth-child (even)"). AddClass ("even");  

Just load all the DOM structures and perform the JS effect before the browser puts all the HTML into DOM tree. Included before loading external pictures and resources.

Its shorthand way:

$ (function () {  
//any need to perform JS effect  
$ ("Table tr:nth-child (even)"). AddClass ("even");  

Use the shorthand for $ (document). Ready (), while the internal JQuery code still uses $ as an alias, regardless of the global $.

JQuery Code:

JQuery (function ($) {
  //You can continue using $ as alias ...
 });

The above use of jquery to load the page to call JS implementation method is small series to share all the content, hope to give you a reference, but also hope that we support cloud habitat community.

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.