我想了解一下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元素吗?
jQuery函数(网站上有完整的列表)在jQuery对象上运行,而不是在DOM元素上运行.可以使用.get()
访问jQuery函数中的DOM元素,也可以直接访问所需索引处的元素:
$("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对象的更多信息,请参阅see the documentation.还可以查看.get()
的文档