jQuery:Firefox focusout事件

vol*_*ron 1 javascript firefox jquery focusout

我在div中有两个输入框,我想在输入的focusOut上隐藏该div,但前提是它们都没有焦点.

这是一个常见的Firefox问题(有人称之为坚持标准),但文档正在窃取重点.

HTML


<div id="baz">
   <input type="text" id="foo" name="foo" />
   <input type="text" id="bar" name="bar" />
</div>
Run Code Online (Sandbox Code Playgroud)

jQuery的


// jQuery Example
jQuery(":input").focusout(function(){
   // Don't do anything if one of the input boxes has focus
   if( jQuery(":input").is( jQuery(document.activeElement) ){ return; }

   // Hide the container if one of the inputs loose focus
   jQuery(this).parents("div").css("display","none");
}
Run Code Online (Sandbox Code Playgroud)

虽然这是一个常见的错误,但我忘记了过去我是如何解决它的.在检查之前,我认为这与设置超时或进行屏幕刷新有关activeElement.


jsFiddle示例

jsFiddle更新(FF4相同问题)

ami*_*t_g 7

演示

jQuery(":input").focusout(function(){
    var elem = jQuery(this).parent("div");
    window.setTimeout(function(){
            // Don't do anything if one of the input boxes has focus
            if( jQuery(":input").is( jQuery(document.activeElement) )){ return; }

            // Hide the container if one of the inputs loose focus
            elem.hide();
    }, 0);
})
Run Code Online (Sandbox Code Playgroud)

演示

jQuery(document).ready(function () {
    var timeoutID;

    jQuery(":input").focus(function () {
        window.clearTimeout(timeoutID);
    }).focusout(function () {
        timeoutID = window.setTimeout(function () {
            jQuery("#baz").hide();
        }, 0);
    });
});
Run Code Online (Sandbox Code Playgroud)