所以我经常遇到这种情况,我想找出一个更好的方法来解决这个问题。我使用命名约定在我的 jQuery 元素前加上 $
以便我可以将它们与 DOM 元素区分开来。例如,以下内容:
$pointers = $('#pointer-box div.pointer');
问题是,如果我想在结果中以 jQuery 的形式获得第 3 个,我必须这样做
$($pointers[2])
这看起来很尴尬。除非有更好的方法吗?
最佳答案
您可以使用 .eq() jQuery 方法
$pointers = $('#pointer-box div.pointer');
var element=$pointers.eq(2);
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.
关于javascript - 如何获取 jQuery 集合中的第 k 个元素...作为 JQUERY 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36656690/