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