Home >Web Front-end >JS Tutorial >Detailed explanation of the use of '$(document).ready(function(){ })' function in Jquery_jquery

Detailed explanation of the use of '$(document).ready(function(){ })' function in Jquery_jquery

WBOY
WBOYOriginal
2016-05-16 17:06:181692browse

Jquery is an excellent Javascrīpt framework. $ is the declaration of the jquery library. It is very unstable (I often encounter it). Change to a stable writing method jQuery.noConflict(); jQuery(document).ready(function( ){});

The advantage of using jQuery is that it packages the operations of various browser versions on DOM objects (you should know the DOM objects of javascript, this is it).

For example, jquery writing:
$("div p"); // (1)
$("div.container"); // (2)
$("div #msg" ); // (3)
$("table a",context); // (4)
$("#myId"); //(5)

The first line of code gets all the

elements under the

tag. The second line of code gets the
element with class container, and the third line of code gets the element with id msg under the
tag. The fourth line of code gets all the connection elements in the table whose context is context. The fifth line of code gets all elements with the id myid

If you are familiar with CSS, you will find these writing methods very familiar! Right. Exactly. You can see the secret. This is how jquery finds the elements in the Dom object. Similar to CSS selectors.

Let’s answer your specific questions now

$(document).ready(function(){
alert("hello");
});(1)

(2)

The above two pieces of code are equivalent. But the advantage of code 1 is to separate performance and logic. And the same operation can be done in different js files, that is, $(document).ready (fn) can appear repeatedly in a page without conflict. Basically, many plugins of Jquery make use of this feature. Because of this feature, multiple plugins can be used together without conflict during initialization.

If we add content in
$(document).ready(function(){
Added content
});
Add content$(".btn-slide").click(function (){
alert("You clicked the link with class equal to btn-slide in the a tag");
});

means that when we click the super link with class=btn-slide, the dialog box "You clicked the link with class equal to btn-slide in the a tag" pops up.

So convenient and easy to use, so using jquery is a good choice.

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