Home  >  Article  >  Web Front-end  >  js(Dom)对象与jquery对象相互转化

js(Dom)对象与jquery对象相互转化

WBOY
WBOYOriginal
2016-06-01 09:55:021564browse

jquery对象转换为js对象。
有两种方法,其原理是一样。
第一种方法:
jquery对象其实是一个数组,可以通过下标获取值,这个值就是js对象。
如:

<code>var hello=$("#hello");//这是一个jquery对象。
var jsHello=$("#hello")[0];//这是一个js对象。</code>


第二种方法:
jQuery本身提供,通过.get(index)方法,得到相应的JS对象 
如:

<code>var hello=$("#hello");//这是一个jquery对象。
var jsHello=$("#hello").get(0);//这是一个js对象。</code>


JS对象转换为JQUERY对象。
对于已经是一个DOM对象,只需要用$()把DOM对象包装起来,就可以获得一个jQuery对象了
如:

<code>var hello=document.getElementById("hello"); //DOM对象 
var jqueryHello=$(hello); //jQuery对象 </code>

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