Maison  >  Article  >  interface Web  >  js(Dom)对象与jquery对象相互转化

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

WBOY
WBOYoriginal
2016-06-01 09:55:021558parcourir

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>

Déclaration:
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn