jQuery对象和DOM元素 [英] jQuery object and DOM element
问题描述
我想了解jQuery对象和DOM元素之间的关系。
I would like to understand relationship between jQuery object and DOM element..
当jQuery返回一个元素时,它显示为 [object Object ]
在警报。
当 getElementByID
返回一个元素,它显示为 [object HTMLDivElement]
。这是什么意思呢?我的意思是他们的对象有区别?
When jQuery returns an element it shows up as [object Object]
in an alert.
When getElementByID
returns an element it shows up as [object HTMLDivElement]
. What does that mean exactly? I mean are both of them objects with a difference ?
还有什么方法可以操作jQuery对象与DOM元素?可以单个jQuery对象代表多个DOM元素吗?
Also what methods can operate on jQuery object vs DOM element? Can a single jQuery object represent multiple DOM elements ?
推荐答案
我想了解jQuery之间的关系对象和DOM元素
I would like to understand relationship between jQuery object and DOM element
jQuery对象是一个包含DOM元素的类数组对象。 jQuery对象可以包含多个DOM元素,具体取决于您使用的选择器。
A jQuery object is an array-like object that contains DOM element(s). A jQuery object can contain multiple DOM elements depending on the selector you use.
还有什么方法可以对jQuery对象与DOM元素进行操作?一个jQuery对象可以表示多个DOM元素?
Also what methods can operate on jQuery object vs DOM element? Can a single jQuery object represent multiple DOM elements ?
jQuery函数(一个完整的列表在网站上)对jQuery对象进行操作对DOM元素。您可以使用 .get()
访问jQuery函数内的DOM元素,或直接访问所需索引的元素:
jQuery functions (a full list is on the website) operate on jQuery objects and not on DOM elements. You can access the DOM elements inside a jQuery function using .get()
or accessing the element at the desired index directly:
$("selector")[0] // Accesses the first DOM element in this jQuery object
$("selector").get(0) // Equivalent to the code above
$("selector").get() // Retrieve a true array of DOM elements matched by this selector
换句话说,以下内容应该得到相同的结果:
In other words, the following should get you the same result:
<div id="foo"></div>
alert($("#foo")[0]);
alert($("#foo").get(0));
alert(document.getElementById("foo"));
有关jQuery对象的更多信息,查看文档。另请参阅 .get()
For more information on the jQuery object, see the documentation. Also check out the documentation for .get()
这篇关于jQuery对象和DOM元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!