Home  >  Article  >  Web Front-end  >  Analysis of DOM loading and event execution examples in JQuery_jquery

Analysis of DOM loading and event execution examples in JQuery_jquery

WBOY
WBOYOriginal
2016-05-16 15:55:231292browse

The examples in this article describe the principles of DOM loading and event execution in JQuery. Share it with everyone for your reference. The specific analysis is as follows:

The interaction between JavaScript and HTML is handled through events raised when the user and browser operate the page. When certain changes or operations occur in the document or some of its elements, the browser automatically generates an event. For example, when the browser loads a document, an event is generated: when the user clicks a button, an event is also generated. Although these interactions can be accomplished using traditional JavaScript events, jQuery adds and extends basic event handling mechanisms. jQuery not only provides a more elegant event handling syntax, but also greatly enhances event handling capabilities.

Take the browser loading a document as an example. After the page is loaded, the browser will add events to the DOM elements through JavaScript. In regular JavaScript code, the window.onload method is usually used, while in jQuery, the $(document).ready() method is used. The $(document).ready() method is the most important function in the event module, which can greatly improve the response speed of web applications. jQuery uses the $(document).ready() method to replace the traditional JavaScript window.onload method. By using this method, you can manipulate the DOM and call the functions it is bound to when it is loaded. During use, you need to pay attention to the subtle differences between the $(document).ready() method and the window.onload method.

Execution timing

$(document).ready() method and window.onload method have similar functions, but there are differences in execution timing. The window.onload method is executed after all elements in the web page (including all associated files of the elements) are completely loaded into the browser, that is, JavaScript can only access any element in the web page at this time. The event handler registered through the $(document).ready() method in jQuery can be called when the DOM is completely ready. At this point, all elements of the web page are accessible to jQuery, but this does not mean that the files associated with these elements have been downloaded.

For example, there is a large photo gallery website that adds certain behaviors to all pictures in the web page, such as hiding or showing the picture after clicking on it. If the window.onload method is used, the user must wait for each image to be loaded before proceeding. If you use the $(document).ready() method in jQuery to set it up, you can operate as long as the DOM is ready, without waiting for all images to be downloaded. Obviously, parsing a web page into a DOM tree is much faster than loading all associated files in the page.

In addition, it should be noted that since the event registered in the $(document).ready() method will be executed as long as the DOM is ready, the associated file of the element may not be downloaded at this time. For example, the HTML related to the image has been downloaded and parsed into a DOM tree, but it is very likely that the image has not been loaded yet, so attributes such as the height and width of the image may not be valid at this time. To solve this problem, you can use another page loading method in jQuery - the load() method. The load() method binds a handler function to the element's onload event. If the handler function is bound to the window object, it will be triggered after all content (including windows, frames, objects, images, etc.) is loaded. If the handler function is bound to an element, it will be triggered after the content of the element is loaded. The jQuery code is as follows:

$(window).load(function(){  
    // 编写代码  
})

is equivalent to the following code in JavaScript:

window.onload = function(){   
    // 编写代码   
})

Now let’s explain in detail the difference between the windows.onload method and the $(document).ready() method:

Assume there are two functions in the web page, the JavaScript code is as follows:

function one(){  
  alert("one");  
}  
function two(){  
  alert ("two");  
}

When the web page is loaded, call the one function and the two function respectively through the following JavaScript code:

window.onload = one;  
window.onload = two;

However, after running the code, it was found that only the string "two" dialog box popped up. The reason why the string "one" dialog box cannot be popped up is that JavaScript's onload event can only save a reference to one function at a time. It will automatically overwrite the previous function with the later function, so new behavior cannot be added to the existing behavior. Behavior.

In order to achieve the effect of sequential triggering of the two functions, we can only create a new JavaScript method to achieve it. The Javascript code is as follows:

window.onload = function(){  
    one();  
    two();  
}

Although writing code in this way can solve some problems, it still cannot meet certain needs. For example, if there are multiple JavaScript files, each file needs to use the window.onload method. In this case, use the method mentioned above. Writing code can be a pain in the ass. The $(document).ready() method of jQuery can handle these situations very well. Each time the $(document).ready() method is called, new behaviors will be appended to the existing behaviors. These behavior functions will be based on the registration. are executed in sequence. For example, the following jQuery code:

function one(){  
    alert("one");  
}  
function two(){  
    alert("two");  
}  
$(document).ready(function(){  
    one();  
})  
$(document).ready(function(){  
    two();  
});

运行代码后,会先弹出字符串“one”对话框,然后弹出字符串“two”对话框。

下面看看ready()与onload()的耗时:

jQuery的ready()耗时 : 498 ms

简写方式

上面我们ready函数写成这样:

$(document).ready(function(){  
    // 编写代码  
})

也可以简写成这样:

$(function(){  
  // 编写代码  
})

另外,$(document)也可以简写为$()。当$()不带参数时,默认参数就是“document”,因此可以简写为:

$().ready(function(){  
    // 编写代码  
})

3种方式都是一样的功能,读者可以根据自己的喜好,选择其中的一种。

希望本文所述对大家的jQuery程序设计有所帮助。

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