简体   繁体   English

jQuery: Firefox 聚焦事件

[英]jQuery: Firefox focusout Event

I've got two input boxes in a div, I want to hide that div on the focusOut of the inputs, but only if both of them do not have focus.我在一个 div 中有两个输入框,我想将该 div 隐藏在输入的 focusOut 上,但前提是它们都没有焦点。

This is a common Firefox problem (some call it adhering to standard), but the document body steals focus between.这是一个常见的 Firefox 问题(有人称其为遵守标准),但文档正文窃取了焦点。

HTML HTML


<div id="baz">
   <input type="text" id="foo" name="foo" />
   <input type="text" id="bar" name="bar" />
</div>

jQuery 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");
}

Though this is a common bug , I forget how I solved it in the past.虽然这是一个常见的错误,但我忘记了我过去是如何解决的。 I think it had something to do with setting a timeout, or doing a screen refresh, before checking for the activeElement .我认为这与在检查activeElement之前设置超时或进行屏幕刷新有关。


jsFiddle Example jsFiddle 示例

jsFiddle Updated (FF4 Same Problem) jsFiddle 更新(FF4 相同问题)

Demo演示

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);
})

Demo演示

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

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

I think amit_g's solution was almost there.我认为 amit_g 的解决方案几乎就在那里。 I vaguely remember that I've went about this in two ways:我依稀记得我已经通过两种方式解决了这个问题:

  1. compare the inputs to the activeElement (the method showed above)将输入与activeElement进行比较(如上所示的方法)
  2. setting/clearing a "focus" class on the element for the respective events在相应事件的元素上设置/清除“焦点”class

I think both methods required using setTimeout , since Firefox has a delayed trigger, which we need to force.我认为这两种方法都需要使用setTimeout ,因为 Firefox 有一个延迟触发,我们需要强制。 While I've heard FF is adhering to standards here, I personally think that standard needs to be amended.虽然我听说 FF 在这里遵守标准,但我个人认为该标准需要修改。


So in addition to adding the timed function call, it also needed to be cleared if the other "acceptable" element gained focus.因此,除了添加定时 function 调用之外,如果其他“可接受”元素获得焦点,也需要将其清除。 The following is not production code but it does show it does serve as an example:以下不是生产代码,但确实表明它确实可以作为示例:

Example Solution示例解决方案

Example Solution (Even Better) - set $debug to false示例解决方案(更好) - 将$debug设置为false

Example Solution (Localized Blocks) - removed debugging clutter示例解决方案(本地化块) - 消除了调试混乱

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM