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

How to convert jquery object to html dom object

青灯夜游
Release: 2023-01-06 11:14:22
Original
3202 people have browsed it

Conversion method: 1. When the jQuery object is a data object, it can be converted through the "[index]" method to get the corresponding DOM object, the syntax is "jQuery object [index]"; 2. Through "jQuery Object.get(index)" statement to convert.

How to convert jquery object to html dom object

The operating environment of this tutorial: windows7 system, jquery1.10.0 version, Dell G3 computer.

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;
Copy after login

Although jQuery objects are generated after wrapping DOM objects, jQuery cannot use DOM objects. Any method, and 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 HTML 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, through the .get(index) method, the corresponding DOM object is obtained

For example:

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

Recommended related video tutorials: jQuery Tutorial (Video)

The above is the detailed content of How to convert jquery object to html dom object. 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