Home  >  Article  >  Web Front-end  >  How to convert jquery object into dom object

How to convert jquery object into dom object

WBOY
WBOYOriginal
2022-06-13 16:32:374047browse

Method: 1. Use the "obj[index]" method, the syntax is "jquery object [the index of the DOM object in the jQuery object]"; 2. Use the "obj.get(index)" method, the syntax is It is "jquery object.get(index of DOM object in jQuery object).innerHTML".

How to convert jquery object into dom object

The operating environment of this tutorial: windows10 system, jquery3.6.0 version, Dell G3 computer.

How to convert a jquery object into a dom object

The jQuery object is an array-like object, and internally uses the DOM object as an array element. There are two ways to convert jQuery objects into DOM objects, namely "obj[index]" and "obj.get(index)". where index represents the index of the DOM object in the jQuery object. These two conversion methods are explained below.

(1)obj[index]

Convert jQuery object into DOM object through obj[index] method. The sample code is as follows.

<div>第1个div</div>
<div>第2个div</div>
<script>
//获取jQuery对象
var divs = $(&#39;div&#39;);
//通过索引的方式,将jQuery对象转换成DOM对象
var div1 = divs[0];
var div2 = divs[1];
//输出div元素的内容
alert(div1.innerHTML);    //输出结果:第1个div
alert(div2. innerHTML);    //输出结果:第2个div
</script>

As can be seen from the above code, a jQuery object can contain multiple DOM elements, and a specific DOM object can be retrieved through indexing.

(2) obj.get(index)

Convert jQuery object into DOM object through obj.get(index) method. The sample code is as follows.

<div>第1个div元素</div>
<script>
var result = $(&#39;div&#39;).get(0).innerHTML;
alert(result);    //输出结果:第1个div元素
</script>

Video tutorial recommendation: jQuery video tutorial

The above is the detailed content of How to convert jquery object into dom object. For more information, please follow other related articles on the PHP Chinese website!

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