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:16 2948browse

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

元素一
元素二
元素三

JavaScript code

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

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