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

Commit 30e760b

Browse files
committed
fix for #4189, live/die now work with contexts other than just document
1 parent dae96f5 commit 30e760b

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

src/event.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -548,13 +548,13 @@ jQuery.fn.extend({
548548
var proxy = jQuery.event.proxy( fn );
549549
proxy.guid += this.selector + type;
550550

551-
jQuery(document).bind( liveConvert(type, this.selector), this.selector, proxy );
551+
jQuery( this.context ).bind( liveConvert(type, this.selector), this.selector, proxy );
552552

553553
return this;
554554
},
555555

556556
die: function( type, fn ){
557-
jQuery(document).unbind( liveConvert(type, this.selector), fn ? { guid: fn.guid + this.selector + type } : null );
557+
jQuery( this.context ).unbind( liveConvert(type, this.selector), fn ? { guid: fn.guid + this.selector + type } : null );
558558
return this;
559559
}
560560
});

test/unit/event.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -489,7 +489,7 @@ test("toggle(Function, Function, ...)", function() {
489489
});
490490

491491
test(".live()/.die()", function() {
492-
expect(49);
492+
expect(52);
493493

494494
var submit = 0, div = 0, livea = 0, liveb = 0;
495495

@@ -561,6 +561,24 @@ test(".live()/.die()", function() {
561561
jQuery("div").die("click");
562562
jQuery("div").die("submit");
563563

564+
// Test binding with a different context
565+
var clicked = 0, container = jQuery('#main')[0];
566+
jQuery("#foo", container).live("click", function(e){ clicked++; });
567+
jQuery("div").trigger('click');
568+
jQuery("#foo").trigger('click');
569+
jQuery("#main").trigger('click');
570+
jQuery("body").trigger('click');
571+
equals( clicked, 2, "live with a context" );
572+
573+
// Make sure the event is actually stored on the context
574+
ok( jQuery.data(container, "events").live, "live with a context" );
575+
576+
// Test unbinding with a different context
577+
jQuery("#foo", container).die("click");
578+
jQuery("#foo").trigger('click');
579+
equals( clicked, 2, "die with a context");
580+
581+
564582
// Verify that return false prevents default action
565583
jQuery("#anchor2").live("click", function(){ return false; });
566584
var hash = window.location.hash;

0 commit comments

Comments
 (0)