Make .add() take an optional context and - if a context is specified in the root selector - use that as the base context.

This commit is contained in:
John Resig 2009-11-07 16:43:31 +01:00
parent ef05f44cce
commit b0fe380cf8
2 changed files with 15 additions and 3 deletions

View file

@ -68,9 +68,9 @@ jQuery.fn.extend({
}); });
}, },
add: function( selector ) { add: function( selector, context ) {
var set = typeof selector === "string" ? var set = typeof selector === "string" ?
jQuery( selector ) : jQuery( selector, context || this.context ) :
jQuery.makeArray( selector ), jQuery.makeArray( selector ),
all = jQuery.merge( this.get(), set ); all = jQuery.merge( this.get(), set );

View file

@ -436,6 +436,18 @@ test("add(String|Element|Array|undefined)", function() {
ok( jQuery([]).add( document.getElementById('form') ).length >= 13, "Add a form (adds the elements)" ); ok( jQuery([]).add( document.getElementById('form') ).length >= 13, "Add a form (adds the elements)" );
}); });
test("add(String, Context)", function() {
expect(6);
equals( jQuery(document).add("#form").length, 2, "Make sure that using regular context document still works." );
equals( jQuery(document.body).add("#form").length, 2, "Using a body context." );
equals( jQuery(document.body).add("#html").length, 1, "Using a body context." );
equals( jQuery(document).add("#form", document).length, 2, "Use a passed in document context." );
equals( jQuery(document).add("#form", document.body).length, 2, "Use a passed in body context." );
equals( jQuery(document).add("#html", document.body).length, 1, "Use a passed in body context." );
});
test("each(Function)", function() { test("each(Function)", function() {
expect(1); expect(1);
var div = jQuery("div"); var div = jQuery("div");