🌐 AI搜索 & 代理 主页
Skip to content

Commit 9de120e

Browse files
committed
Added support for .eq(-N), .first(), and .last(). Fixes #2164 and #4188.
1 parent 4ea4fad commit 9de120e

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

src/traversing.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,17 @@ jQuery.fn.extend({
7878
},
7979

8080
eq: function( i ) {
81-
return this.slice( i, +i + 1 );
81+
return i === -1 ?
82+
this.slice( i ) :
83+
this.slice( i, +i + 1 );
84+
},
85+
86+
first: function() {
87+
return this.eq( 0 );
88+
},
89+
90+
last: function() {
91+
return this.eq( -1 );
8292
},
8393

8494
slice: function() {

test/unit/traversing.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ test("prev([String])", function() {
203203
});
204204

205205
test("slice()", function() {
206-
expect(6);
206+
expect(7);
207207

208208
var $links = jQuery("#ap a");
209209

@@ -213,8 +213,20 @@ test("slice()", function() {
213213
isSet( $links.slice(-1), q("mark"), "slice(-1)" );
214214

215215
isSet( $links.eq(1), q("groups"), "eq(1)" );
216-
217216
isSet( $links.eq('2'), q("anchor1"), "eq('2')" );
217+
isSet( $links.eq(-1), q("mark"), "eq(-1)" );
218+
});
219+
220+
test("first()/last()", function() {
221+
expect(4);
222+
223+
var $links = jQuery("#ap a"), $none = jQuery("asdf");
224+
225+
isSet( $links.first(), q("google"), "first()" );
226+
isSet( $links.last(), q("mark"), "last()" );
227+
228+
isSet( $none.first(), [], "first() none" );
229+
isSet( $none.last(), [], "last() none" );
218230
});
219231

220232
test("map()", function() {

0 commit comments

Comments
 (0)