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

Commit b0fe380

Browse files
committed
Make .add() take an optional context and - if a context is specified in the root selector - use that as the base context.
1 parent ef05f44 commit b0fe380

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

src/traversing.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,9 @@ jQuery.fn.extend({
6868
});
6969
},
7070

71-
add: function( selector ) {
71+
add: function( selector, context ) {
7272
var set = typeof selector === "string" ?
73-
jQuery( selector ) :
73+
jQuery( selector, context || this.context ) :
7474
jQuery.makeArray( selector ),
7575
all = jQuery.merge( this.get(), set );
7676

@@ -185,4 +185,4 @@ jQuery.extend({
185185

186186
return r;
187187
}
188-
});
188+
});

test/unit/core.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,18 @@ test("add(String|Element|Array|undefined)", function() {
436436
ok( jQuery([]).add( document.getElementById('form') ).length >= 13, "Add a form (adds the elements)" );
437437
});
438438

439+
test("add(String, Context)", function() {
440+
expect(6);
441+
442+
equals( jQuery(document).add("#form").length, 2, "Make sure that using regular context document still works." );
443+
equals( jQuery(document.body).add("#form").length, 2, "Using a body context." );
444+
equals( jQuery(document.body).add("#html").length, 1, "Using a body context." );
445+
446+
equals( jQuery(document).add("#form", document).length, 2, "Use a passed in document context." );
447+
equals( jQuery(document).add("#form", document.body).length, 2, "Use a passed in body context." );
448+
equals( jQuery(document).add("#html", document.body).length, 1, "Use a passed in body context." );
449+
});
450+
439451
test("each(Function)", function() {
440452
expect(1);
441453
var div = jQuery("div");

0 commit comments

Comments
 (0)