How jQuery works (1)

Source: Internet
Author: User

1. $ (document). Read (fn)Differences from window. onload = fnWindow. onload = fn is executed only after the page is fully loaded. Complete loading of images, videos, and other relatively slow resources is completed before execution. Therefore, if we have some JavaScript code that starts execution after page loading, the page may be messy. After all these resources are loaded, the page will change as we want. The $ (document). read (fn) of jQuery only needs to be executed after the DOM element is loaded on the page, so that the user does not need to see a deformed page.2. jQuery page loading write method and $ conflict ProcessingTo complete DOM loading and execute JS Code, we can use jQuery's ready function. JQuery provides two writing methods: $ (document). ready (fucntion () {// Your code
}); Or $ (function () {// Your code
}) In the above example, we use jQuery $ to replace jQuery objects. However, sometimes we see other JavaScript libraries also use $ to replace library objects. Therefore, in jQuery, you can either directly use the jQuery object instead of $ or use the method provided by the jQuery object:

 
$.noConflict(); 
jQuery(document).ready(function($) { 
// Code that uses jQuery's $ can follow here. 
}); 

Supplement: jQuery blocks eventsWhen creating a click event, we may sometimes use <a href = "#"> Show Pop-up </a>, however, when we click the link, the page will jump. The previous method is <a href = \ '# \' "Pop-up </a>. But what should we do when jQuery uses events to dynamically define events for elements? The following code:
 
$( document ).ready(function() {$ ("A"). click (function (event) {alert ("As you can see, the link no longer took you to jquery.com "); 
 event.preventDefault();});}); 


This article is from the "SG-YYZ" blog, please be sure to keep this source http://sgyyz.blog.51cto.com/5069360/1297945

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.