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

How to convert html dom object to jquery object

青灯夜游
青灯夜游Original
2021-05-19 17:19:163052browse

Conversion method: If it is already a DOM object, you only need to wrap the DOM object with "$()" to get a jQuery object. The specific syntax format is "$(DOM object)".

How to convert html dom object to jquery object

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

Convert DOM object to jQuery object:

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

For example:

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

After processing the ordinary DOM object into a jQuery object through the $(DOM) method, we can call the jQuery method.

HTML code

<div>元素一</div><div>元素二</div><div>元素三</div>

JavaScript code

var div=document.getElementsByTagName(&#39;div&#39;);//DOM对象
var $div=$(div);//jQuery对象
var $first=$div.frist();//找到第一个div元素
$first.css(&#39;color&#39;,&#39;red&#39;);//给第一个元素设置颜色

Get the elements of the div node through getElementsByTagName. The result is a DOM collection object, but this object is an array collection (3 div elements). Convert it into a jQuery object through the $(div) method, find the first element and change its color by calling the first and css methods in the jQuery object.

Recommended related video tutorials: jQuery Tutorial (Video)

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