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

How to convert jquery object to js object

青灯夜游
Release: 2021-11-12 15:52:42
Original
5126 people have browsed it

Methods to convert jquery objects into js objects: 1. Use the "jquery object [index]" statement to convert; 2. Use the "jquery object.get(index)" statement to convert.

How to convert jquery object to js object

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

JS type objects and jquery type objects are two completely different objects. However, the methods of the two objects cannot call each other. So what should I do if the js object wants to call a method in jquery, or if the jquery object wants to call a js method? This time involves the issue of mutual conversion between js objects and jquery objects.

For example:

document.getElementById("text").hide();

Cannot be implemented because hide() is a jquery object method, the js object cannot be called

$("#text2").innerHTML = "jredu";

The same cannot be achieved, because innerHTML It is an attribute of the js object and cannot be used by the jquery object.

Then, let me introduce to you how to realize the mutual conversion between js and jQuery:

---Conversion 1: Convert jquery object into js object

- --Conversion 2: Convert js object to jquery object

Convert jquery object to js object

There are two conversion methods to convert a jQuery object into js object:[index] and .get(index);

(1) The jQuery object is a data object that can be accessed through the [index] method , to get the corresponding js object.

For example:

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

(2)jQuery itself provides it, through the .get(index) method, the corresponding js object is obtained

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

Convert js object to jquery object

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

Syntax: $(js object)

For example:

var v=document.getElementById("v"); //js对象 
var $v=$(v); //jQuery对象
Copy after login

After conversion, you can use any jQuery method.

Recommended related video tutorials: jQuery Tutorial (Video)

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