简体   繁体   中英

Is there are similar method to .get() but one returning a jQuery object instead of a DOM one?

I was wondering if the people over at jQuery implemented an alternative method to .get() , as in one that returns a jQuery object instead of a DOM node.

This way, I don't have to

  1. wrap the object retrieved using the .get() method with an additional $() call and
  2. write my own jQuery plugin in order to provide that functionality.

I especially don't want to do #2 because I will pretty much be reinventing the wheel, when a functionality already exists.

you can use .eq(index), which returns a jquery instance. For example

var mycoll = $("div.myDivs")
var firstElementOfCollection = mycoll.eq(0);

or as ThiefMaster pointed out in his comment below, these will work as well (and are potentially nicer depending what you are doing :))

$("div.myDivs:eq(0)");
$("div.myDivs:first");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM