Heim  >  Artikel  >  Web-Frontend  >  js(Dom)对象与jquery对象相互转化

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

WBOY
WBOYOriginal
2016-06-01 09:55:021558Durchsuche

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>

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn