Home > Web Front-end > JS Tutorial > body text

What are jQuery objects? Detailed explanation of how to convert jQuery objects into DOM object instances

伊谢尔伦
Release: 2017-07-20 13:13:45
Original
2075 people have browsed it

When you first start learning jQuery, you may not be able to tell which are jQuery objects and which are DOM objects. As for the DOM object, there is not much explanation. We have come into contact with too many. Let’s focus on jQuery and the conversion between the two.

What is a jQuery object?

---It is the object generated by wrapping the DOM object through jQuery. The jQuery object is unique to jQuery and can use methods in jQuery.

For example:

$("#test").html() means: get the html code in the element with ID test. Among them, html() is a method in jQuery

This code is equivalent to using DOM to implement the code:

document.getElementById("id").innerHTML;
Although jQuery objects are generated after wrapping DOM objects, jQuery cannot use any methods of DOM objects. Similarly, DOM objects cannot use methods in jQuery. If used indiscriminately, an error will be reported. For example: $("#test").innerHTML, document.getElementById("id").html() and other writing methods are wrong.

Another thing to note is that the jQuery object obtained by using #id as the selector and the DOM object obtained by document.getElementById("id") are not equivalent. Please see the conversion between the two below.

Since jQuery is different but also related, jQuery objects and DOM objects can also be converted to each other. Before converting the two, we first make a convention: if a jQuery object is obtained, then we add $ in front of the variable, such as: var $variab = jQuery object; if a DOM object is obtained, it is the same as usual. : var variab = DOM object; this convention is only for convenience of explanation and distinction, and is not stipulated in actual use.

Convert jQuery object to DOM object:

Two conversion methods convert a jQuery object into DOM object: [index] and .get(index);

(1) The jQuery object is a data object, and the corresponding DOM object can be obtained through the [index] method.

For example:


var $v =$("#v") ; //jQuery对象
var v=$v[0]; //DOM对象
alert(v.checked) //检测这个checkbox是否被选中
Copy after login

(2)jQuery itself provides the corresponding DOM object through the .get(index) method

For example:


var $v=$("#v"); //jQuery对象
var v=$v.get(0); //DOM对象
alert(v.checked) //检测这个checkbox是否被选中
Copy after login

DOM object converted into jQuery object:

For already a DOM object, you only need to wrap the DOM object with $() to get a jQuery object. $(DOM object)

For example:


var v=document.getElementById("v"); //DOM对象
var $v=$(v); //jQuery对象
Copy after login

After conversion, you can use any jQuery method.

Through the above methods, jQuery objects and DOM objects can be converted to each other at will. What needs to be emphasized again is that only DOM objects can use methods in DOM, and jQuery objects cannot use methods in DOM.

The above is the detailed content of What are jQuery objects? Detailed explanation of how to convert jQuery objects into DOM object instances. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template