[英]jQuery: Firefox focusout Event
我在一个 div 中有两个输入框,我想将该 div 隐藏在输入的 focusOut 上,但前提是它们都没有焦点。
这是一个常见的 Firefox 问题(有人称其为遵守标准),但文档正文窃取了焦点。
<div id="baz">
<input type="text" id="foo" name="foo" />
<input type="text" id="bar" name="bar" />
</div>
// 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");
}
虽然这是一个常见的错误,但我忘记了我过去是如何解决的。 我认为这与在检查activeElement
之前设置超时或进行屏幕刷新有关。
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);
})
jQuery(document).ready(function () {
var timeoutID;
jQuery(":input").focus(function () {
window.clearTimeout(timeoutID);
}).focusout(function () {
timeoutID = window.setTimeout(function () {
jQuery("#baz").hide();
}, 0);
});
});
我认为 amit_g 的解决方案几乎就在那里。 我依稀记得我已经通过两种方式解决了这个问题:
activeElement
进行比较(如上所示的方法) 我认为这两种方法都需要使用setTimeout
,因为 Firefox 有一个延迟触发,我们需要强制。 虽然我听说 FF 在这里遵守标准,但我个人认为该标准需要修改。
因此,除了添加定时 function 调用之外,如果其他“可接受”元素获得焦点,也需要将其清除。 以下不是生产代码,但确实表明它确实可以作为示例:
$debug
设置为false
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.