Skip to content

Commit e761e0c

Browse files
gibson042timmywil
authored andcommitted
Support event delegation with relative selectors. Fixes #10762. Closes jquerygh-860.
1 parent a08a18b commit e761e0c

File tree

2 files changed

+14
-11
lines changed

2 files changed

+14
-11
lines changed

src/traversing.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
var runtil = /Until$/,
22
rparentsprev = /^(?:parents|prev(?:Until|All))/,
33
isSimple = /^.[^:#\[\.,]*$/,
4-
POS = jQuery.expr.match.globalPOS,
4+
rneedsContext = jQuery.expr.match.needsContext,
55
// methods guaranteed to produce a unique set when starting from a unique set
66
guaranteedUnique = {
77
children: true,
@@ -72,9 +72,9 @@ jQuery.fn.extend({
7272
is: function( selector ) {
7373
return !!selector && (
7474
typeof selector === "string" ?
75-
// If this is a positional selector, check membership in the returned set
75+
// If this is a positional/relative selector, check membership in the returned set
7676
// so $("p:first").is("p:last") won't return true for a doc with two "p".
77-
POS.test( selector ) ?
77+
rneedsContext.test( selector ) ?
7878
jQuery( selector, this.context ).index( this[0] ) >= 0 :
7979
jQuery.filter( selector, this ).length > 0 :
8080
this.filter( selector ).length > 0 );
@@ -85,7 +85,7 @@ jQuery.fn.extend({
8585
i = 0,
8686
l = this.length,
8787
ret = [],
88-
pos = POS.test( selectors ) || typeof selectors !== "string" ?
88+
pos = rneedsContext.test( selectors ) || typeof selectors !== "string" ?
8989
jQuery( selectors, context || this.context ) :
9090
0;
9191

test/unit/event.js

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2342,17 +2342,20 @@ test("jQuery.off using dispatched jQuery.Event", function() {
23422342
.remove();
23432343
});
23442344

2345-
test( "delegated event with delegateTarget-relative selector (#)", function() {
2346-
expect(1);
2347-
var markup = jQuery( '<ul><li><ul id="u1"><li id="f1"></li></ul></li>' ).appendTo("body");
2345+
test( "delegated event with delegateTarget-relative selector", function() {
2346+
expect(2);
2347+
var markup = jQuery( '<ul><li><a id="a0"></a><ul id="ul0"><li><a id="a0_0"></a></li><li><a id="a0_1"></a></li></ul></li></ul>' ).appendTo("body");
23482348

23492349
markup
2350-
.find("#u1")
2351-
.on( "click", "li:first", function() {
2352-
ok( this.id === "f1" , "first li under #u1 was clicked" );
2350+
.on( "click", ">li>a", function() {
2351+
ok( this.id === "a0", "child li was clicked" );
2352+
})
2353+
.find("#ul0")
2354+
.on( "click", "li:first>a", function() {
2355+
ok( this.id === "a0_0" , "first li under #u10 was clicked" );
23532356
})
2354-
.find("#f1").click().end()
23552357
.end()
2358+
.find("a").click().end()
23562359
.remove();
23572360
});
23582361

0 commit comments

Comments
 (0)