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.
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是否被选中
(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是否被选中
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对象
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!