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

Conversion method between jQuery object and DOM object_jquery

WBOY
Release: 2016-05-16 18:29:22
Original
752 people have browsed it
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 the jQuery object is a wrapper It is generated after the DOM object, but jQuery cannot use any method of the DOM object. Similarly, the DOM object cannot use the 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 )jQuery object is a data object, and the corresponding DOM object can be obtained through the [index] method.
For example: var $v =$("#v"); //jQuery object
var v=$v[0]; //DOM object
alert(v.checked) //Detect this Whether the checkbox is selected
(2) jQuery itself provides, through the .get(index) method, the corresponding DOM object
is obtained, such as: var $v=$("#v"); //jQuery object
var v=$v.get(0); //DOM object
alert(v.checked) //Detect whether this checkbox is selected

DOM object converted into jQuery object:
For 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 object
var $v=$(v); //jQuery object
can be converted Feel free to use jQuery's methods.
Through the above methods, jQuery objects and DOM objects can be converted to each other arbitrarily. 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 following are other related usage methods:
1. Convert DOM object to jQuery object
Ordinary Dom objects can generally be converted into jQuery objects through $().

For example: $(document.getElementById("msg"))
What is returned is the jQuery object, and jQuery methods can be used.

2. Convert jQuery object to DOM object
Because the jQuery object itself is a collection. Therefore, if the jQuery object is to be converted into a Dom object, one of the items must be retrieved, which can generally be retrieved through an index.
For example: $("#msg")[0], $("div").eq(1)[0], $("div").get()[1], $("td" )[5]

These are Dom objects. You can use methods in Dom, but you can no longer use jQuery methods.

The following writing methods are correct:
$("#msg").html();
$("#msg")[0].innerHTML;
$ ("#msg").eq(0)[0].innerHTML;
$("#msg").get(0).innerHTML;
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