有没有办法拥有 get()
返回 jQuery 对象而不是“仅仅”DOM 元素?
示例:
$("div").get(0)
返回[<div></div>]
代替<div></div>
.
我想防止像$($("div).get(0))
这样的过度包装因为查询会比示例长一点,我担心可读性会丢失。我也不想使用变量在 RAM 中保存不必要的 DOM 元素。
最佳答案
使用eq()返回 jquery 对象而不是 get()
$("div").eq(0)
Given a jQuery object that represents a set of DOM elements, the .eq() method constructs a new jQuery object from one element within that set. The supplied index identifies the position of this element in the set, jQuery doc
关于javascript - 让 get() 返回一个 jQuery 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21707413/