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

Commit d6991fa

Browse files
committed
Added support for .closest(filter, DOMElement). Fixes #4072.
1 parent 61b18c8 commit d6991fa

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

src/traversing.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,13 @@ jQuery.fn.extend({
5151
return this.pushStack( jQuery.winnow(this, selector, true), "filter", selector );
5252
},
5353

54-
closest: function( selector ) {
54+
closest: function( selector, context ) {
5555
var pos = jQuery.expr.match.POS.test( selector ) ? jQuery(selector) : null,
5656
closer = 0;
5757

5858
return this.map(function(){
5959
var cur = this;
60-
while ( cur && cur.ownerDocument ) {
60+
while ( cur && cur.ownerDocument && cur !== context ) {
6161
if ( pos ? pos.index(cur) > -1 : jQuery(cur).is(selector) ) {
6262
jQuery.data(cur, "closest", closer);
6363
return cur;

test/unit/traversing.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,14 +91,20 @@ test("filter(jQuery)", function() {
9191
})
9292

9393
test("closest()", function() {
94-
expect(6);
94+
expect(9);
9595
isSet( jQuery("body").closest("body").get(), q("body"), "closest(body)" );
9696
isSet( jQuery("body").closest("html").get(), q("html"), "closest(html)" );
9797
isSet( jQuery("body").closest("div").get(), [], "closest(div)" );
9898
isSet( jQuery("#main").closest("span,#html").get(), q("html"), "closest(span,#html)" );
9999

100100
isSet( jQuery("div:eq(1)").closest("div:first").get(), [], "closest(div:first)" );
101101
isSet( jQuery("div").closest("body:first div:last").get(), q("fx-tests"), "closest(body:first div:last)" );
102+
103+
// Test .closest() limited by the context
104+
var jq = jQuery("#nothiddendivchild");
105+
isSet( jq.closest("html", document.body).get(), [], "Context limited." );
106+
isSet( jq.closest("body", document.body).get(), [], "Context limited." );
107+
isSet( jq.closest("#nothiddendiv", document.body).get(), q("nothiddendiv"), "Context not reached." );
102108
});
103109

104110
test("not(Selector)", function() {

0 commit comments

Comments
 (0)