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

Commit 03481a5

Browse files
Jörn Zaeffererjeresig
authored andcommitted
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.
1 parent 5dc6b7c commit 03481a5

File tree

3 files changed

+39
-17
lines changed

3 files changed

+39
-17
lines changed

src/event.js

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -720,24 +720,23 @@ function trigger( type, elem, args ) {
720720
}
721721

722722
// Create "bubbling" focus and blur events
723-
if ( !jQuery.support.focusBubbles ) {
723+
if ( document.addEventListener ) {
724+
jQuery.each({ focus: "focusin", blur: "focusout" }, function( orig, fix ){
725+
jQuery.event.special[ fix ] = {
726+
setup: function() {
727+
this.addEventListener( orig, handler, true );
728+
},
729+
teardown: function() {
730+
this.removeEventListener( orig, handler, true );
731+
}
732+
};
724733

725-
jQuery.each({ focus: "focusin", blur: "focusout" }, function( orig, fix ){
726-
jQuery.event.special[ orig ] = {
727-
setup: function() {
728-
jQuery.event.add( this, fix, ieHandler );
729-
},
730-
teardown: function() {
731-
jQuery.event.remove( this, fix, ieHandler );
734+
function handler( e ) {
735+
e = jQuery.event.fix( e );
736+
e.type = fix;
737+
return jQuery.event.handle.call( this, e );
732738
}
733-
};
734-
735-
function ieHandler() {
736-
arguments[0].type = orig;
737-
return jQuery.event.handle.apply(this, arguments);
738-
}
739-
});
740-
739+
});
741740
}
742741

743742
jQuery.each(["bind", "one"], function(i, name) {

src/support.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,6 @@
111111

112112
jQuery.support.submitBubbles = eventSupported("submit");
113113
jQuery.support.changeBubbles = eventSupported("change");
114-
jQuery.support.focusBubbles = eventSupported("focus");
115114

116115
// release memory in IE
117116
root = script = div = all = a = null;

test/delegatetest.html

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,24 @@ <h2>Change Tests</h2>
7575
<td id='textbind' class="red">TEXT</td>
7676
<td id='textareabind' class="red">TEXTAREA</td>
7777
</tr>
78+
<tr>
79+
<td>Focusin:</td>
80+
<td id='selectfocus' class="red">SELECT</td>
81+
<td id='mselectfocus' class="red">MULTI</td>
82+
<td id='checkboxfocus' class="red">CHECKBOX</td>
83+
<td id='radiofocus' class="red">RADIO</td>
84+
<td id='textfocus' class="red">TEXT</td>
85+
<td id='textareafocus' class="red">TEXTAREA</td>
86+
</tr>
87+
<tr>
88+
<td>Focusout:</td>
89+
<td id='selectblur' class="red">SELECT</td>
90+
<td id='mselectblur' class="red">MULTI</td>
91+
<td id='checkboxblur' class="red">CHECKBOX</td>
92+
<td id='radioblur' class="red">RADIO</td>
93+
<td id='textblur' class="red">TEXT</td>
94+
<td id='textareablur' class="red">TEXTAREA</td>
95+
</tr>
7896
</table>
7997
<h2>Submit Tests</h2>
8098
<table>
@@ -112,6 +130,12 @@ <h2>Submit Tests</h2>
112130

113131
<script type='text/javascript'>
114132
jQuery.fn.addChangeTest = function( id, prevent ) {
133+
this.bind("focusin", function(){
134+
jQuery(id + "focus").blink();
135+
}).bind("focusout", function(){
136+
jQuery(id + "blur").blink();
137+
});
138+
115139
return this.bind("change", function(e){
116140
jQuery(id + "bind").blink();
117141
}).live("change", function(e){

0 commit comments

Comments
 (0)