From: Jörn Zaefferer Date: Mon, 21 Dec 2009 21:10:21 +0000 (-0500) Subject: Stop trying to emulate the focus/blur event in IE, doesn't work as one might expect... X-Git-Url: http://git.asbjorn.it/?p=jquery.git;a=commitdiff_plain;h=03481a52c72e417b01cfeb499f26738cf5ed5839 Stop trying to emulate the focus/blur event in IE, doesn't work as one might expect, anyway. Instead, implement the focusin/focusout events in all other browsers - which creates a much better parity across all browsers. Uses event capturing instead of bubbling to make it happen. Thanks to Alexander for the recommendation and to Joern Zaefferer for the original focus/blur delegation code. --- diff --git a/src/event.js b/src/event.js index 983e9e5..3d2be68 100644 --- a/src/event.js +++ b/src/event.js @@ -720,24 +720,23 @@ function trigger( type, elem, args ) { } // Create "bubbling" focus and blur events -if ( !jQuery.support.focusBubbles ) { +if ( document.addEventListener ) { + jQuery.each({ focus: "focusin", blur: "focusout" }, function( orig, fix ){ + jQuery.event.special[ fix ] = { + setup: function() { + this.addEventListener( orig, handler, true ); + }, + teardown: function() { + this.removeEventListener( orig, handler, true ); + } + }; -jQuery.each({ focus: "focusin", blur: "focusout" }, function( orig, fix ){ - jQuery.event.special[ orig ] = { - setup: function() { - jQuery.event.add( this, fix, ieHandler ); - }, - teardown: function() { - jQuery.event.remove( this, fix, ieHandler ); + function handler( e ) { + e = jQuery.event.fix( e ); + e.type = fix; + return jQuery.event.handle.call( this, e ); } - }; - - function ieHandler() { - arguments[0].type = orig; - return jQuery.event.handle.apply(this, arguments); - } -}); - + }); } jQuery.each(["bind", "one"], function(i, name) { diff --git a/src/support.js b/src/support.js index 91ee77d..249fb15 100644 --- a/src/support.js +++ b/src/support.js @@ -111,7 +111,6 @@ jQuery.support.submitBubbles = eventSupported("submit"); jQuery.support.changeBubbles = eventSupported("change"); - jQuery.support.focusBubbles = eventSupported("focus"); // release memory in IE root = script = div = all = a = null; diff --git a/test/delegatetest.html b/test/delegatetest.html index e3ebbfc..7b35c8e 100644 --- a/test/delegatetest.html +++ b/test/delegatetest.html @@ -75,6 +75,24 @@ TEXT TEXTAREA + + Focusin: + SELECT + MULTI + CHECKBOX + RADIO + TEXT + TEXTAREA + + + Focusout: + SELECT + MULTI + CHECKBOX + RADIO + TEXT + TEXTAREA +

Submit Tests

@@ -112,6 +130,12 @@