jQuery Focus 在 Firefox 上失败

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7046798/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 23:20:03  来源:igfitidea点击:

jQuery Focus fails on firefox

jqueryfirefox

提问by Madara's Ghost

I've been some testing reserching for this other question, when I noticed something very peculiar. FF4/5 fail to fire the focusjQuery event. The other questionwhich might be considered a duplicate was closed and accepted without a real answer.

当我注意到一些非常奇怪的事情时,我一直在对另一个问题进行一些测试研究。FF4/5 无法触发focusjQuery 事件。另一个可能被视为重复的问题已关闭并在没有真正答案的情况下被接受。

For the question itself, I tried the following simple bit of code:

对于问题本身,我尝试了以下简单的代码:

$('#target').focusout(function() {
    $(this).focus();
});

It works well in Chrome and in IE, but it fails on FF. Here's the jsFiddlefor the lazy ones among us.

它在 Chrome 和 IE 中运行良好,但在 FF 中失败。这是我们中间懒人的jsFiddle

Can anyone explain this behavior? Or is it a known bug?

谁能解释这种行为?或者它是一个已知的错误?

回答by chaos

I think I ran into this before, and if I recall correctly it seemed to be some kind of reentrancy problem. My impression was that because FF is already in the process of transitioning focus, it won't let you initiate another focus transition. I believe my workaround was something like

我想我以前遇到过这个问题,如果我没记错的话,这似乎是某种可重入问题。我的印象是,因为FF已经在转移焦点的过程中,它不会让你发起另一个焦点转移。我相信我的解决方法是这样的

$('#target').focusout(function() {
    setTimeout(function() {
        $(this).focus();
    }, 0);
});

回答by ain

The manualsays aboult .focus()call

手册说aboult.focus()通话

This method is a shortcut for .trigger('focus')

此方法是 .trigger('focus') 的快捷方式

and from the .trigger()topic

并从.trigger()主题

Although .trigger() simulates an event activation, complete with a synthesized event object, it does not perfectly replicate a naturally-occurring event.

尽管 .trigger() 模拟事件激活,并带有合成的事件对象,但它并不能完美地复制自然发生的事件。

So as I understand it, the call $(this).focus();is supposed to trigger the OnFocus event (if there is one attached to the object) but is not quaranteed to actually set/change the focused object.

因此,据我了解,该调用$(this).focus();应该触发 OnFocus 事件(如果有一个附加到对象),但不保证实际设置/更改聚焦对象。

回答by daniel

This worked for me in Firefox 38. I needed to test different delay ms. Thanks to @camaleo comment.

这在 Firefox 38 中对我有用。我需要测试不同的延迟毫秒。感谢@camaleo 评论。

$(document).ready(function() {
setTimeout(function() { $('#myid').focus(); }, 100);
});

回答by Rafa?l De Jongh

The focus seems to work now in the latest Firefox without the need of the setTimeout function.

焦点现在似乎可以在最新的 Firefox 中工作,而无需 setTimeout 函数。

If you want to also select the input field you will have to make use of the .select() function though as document.execCommand('SelectAll'); doesn't seem to work on Firefox either.

如果您还想选择输入字段,则必须使用 .select() 函数作为 document.execCommand('SelectAll'); 似乎也不适用于 Firefox。

So having the input field first focused and then selected you then can copy it or do whatever you want with it.

因此,首先将输入字段聚焦然后选择,然后您可以复制它或使用它做任何您想做的事情。

In my use case I required to copy the url from the input field if someone pressed on the copy button:

在我的用例中,如果有人按下复制按钮,我需要从输入字段复制 url:

$(".copyURL").click(function(){ 
    $(this).prev().focus().select();
    document.execCommand("Copy",false,null);
});

I hope this could help anyone else who's searching for this problem!

我希望这可以帮助其他正在寻找此问题的人!