« 新站上线的手记 Discuz!多附件上传选择框之jQuery版 »

jQuery对象与dom对象的转换

最近在闲暇时间用jQuery搞了一个多文件上传的东东,顺便写点笔记。

一直以来对于通过jQuery方式获取的对象使不能直接使用JavaScript的一些方法的,开始的时候不理解,现在此案知道,原来jQuery获得的对象并不和我们平时使用getElementById获得的对象是一样的对象。所以一些新手就很迷惑,为什么${”#Element”}不能直接innerHTML,这就是原因所在,解决方式请看下文。

jQuery对象与dom对象的转换
只有jquery对象才能使用jquery定义的方法。注意dom对象和jquery对象是有区别的,调用方法时要注意操作的是dom对象还是jquery对象。
普通的dom对象一般可以通过$()转换成jquery对象。

如:

  1. $(document.getElementById("msg"))

则为jquery对象,可以使用jquery的方法。
由于jquery对象本身是一个集合。所以如果jquery对象要转换为dom对象则必须取出其中的某一项,一般可通过索引取出。
如:

  1. $("#msg")[0],$("div").eq(1)[0],$("div").get()[1],$("td")[5]

这些都是dom对象,可以使用dom中的方法,但不能再使用Jquery的方法。
以下几种写法都是正确的:

  1. $("#msg").html();
  2. $("#msg")[0].innerHTML;
  3. $("#msg").eq(0)[0].innerHTML;
  4. $("#msg").get(0).innerHTML;

“jQuery对象与dom对象的转换”

7条回复
  1. 什么是jQuery….

  2. jQuery is a new type of Javascript library. It is not a huge, bloated framework promising the best in AJAX - nor is it just a set of needlessly complex enhancements -
    jQuery is designed to change the way that you write Javascript.

  3. 谢谢

  4. so good, jquery’s web is terrible.

  5. 妈妈说不要和木JJ的人随便说话

  6. 谢谢,明白了

  7. 转走了

回复留言

*
To prove you're a person (not a spam script), type the security word shown in the picture.
Anti-Spam Image

你可以使用XHTML标签: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>