Home > Article > Web Front-end > How to convert jquery object into dom object
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".
The operating environment of this tutorial: windows10 system, jquery3.6.0 version, Dell G3 computer.
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 = $('div'); //通过索引的方式,将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 = $('div').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!