]> source.dussan.org Git - jquery.git/commitdiff
Added support for the new .closest() method (very useful for event delegation).
authorJohn Resig <jeresig@gmail.com>
Mon, 22 Dec 2008 00:52:53 +0000 (00:52 +0000)
committerJohn Resig <jeresig@gmail.com>
Mon, 22 Dec 2008 00:52:53 +0000 (00:52 +0000)
src/core.js
test/unit/core.js

index 7ddb2c5b0fdde1b3c50c8108d57795a019f12bc3..ff8114bb5ff1687385f74225b71ed22cb10996f1 100644 (file)
@@ -338,6 +338,17 @@ jQuery.fn = jQuery.prototype = {
                        }) ), "filter", selector );
        },
 
+       closest: function( selector ) {
+               return this.map(function(){
+                       var cur = this;
+                       while ( cur && cur.ownerDocument ) {
+                               if ( jQuery(cur).is(selector) )
+                                       return cur;
+                               cur = cur.parentNode;
+                       }
+               });
+       },
+
        not: function( selector ) {
                if ( typeof selector === "string" )
                        // test special case where just one selector is passed in
index 536e58cf5b9bc6a5d065b802c75b0e842581da87..1ae2dfa5f93421c81da88e57d9ae3819cc15037c 100644 (file)
@@ -1309,6 +1309,14 @@ test("filter()", function() {
        equals( j.filter("[name]").length, 0, "Check node,textnode,comment to filter the one span" );
 });
 
+test("closest()", function() {
+       expect(4);
+       isSet( jQuery("body").closest("body").get(), q("body"), "closest(body)" );
+       isSet( jQuery("body").closest("html").get(), q("html"), "closest(html)" );
+       isSet( jQuery("body").closest("div").get(), [], "closest(div)" );
+       isSet( jQuery("#main").closest("span,#html").get(), q("html"), "closest(span,#html)" );
+});
+
 test("not()", function() {
        expect(8);
        equals( jQuery("#main > p#ap > a").not("#google").length, 2, "not('selector')" );