使用jQuery的.focus()在Mac上的Firefox中设置焦点

时间:2014-11-28 04:03:48

标签: javascript jquery firefox

我正在尝试让.focus()在Mac上使用Firefox(版本33.1.1)。一些类似的问题提到了.focus()没有视觉效果的解决方案;在下面的每个测试中,我甚至无法实现这一点。

下面列出的各种测试都适用于Chrome,但不适用于Firefox。使用的HTML是:

<input type="text" id="test" tabindex="1">

设置tabindex基于this suggestion,但似乎没有任何效果。

我还考虑使用autofocus,但这不起作用,因为我需要在用户使用网页时操纵多个字段的焦点,而不仅仅是加载单个字段。

更新Rhumborl建议below该问题可能需要在使用iframe时对Firefox的焦点事件进行处理,而且肯定会出现问题。当您从JSFiddle的iframe“编辑小提琴”窗口中取出它而不是查看输出全屏时,以下每个测试都有效。下面我添加了每个工作全屏版本的链接。

一种。 .focus()

$("#test").focus();

Fiddle更新this works in full screen)。

B中。 setTimeout .focus()

setTimeout(function(){
    $("#test").focus();
},100);

Fiddle更新this works in full screen)。根据建议hereherehere

℃。 .trigger('focus')

$("#test").trigger('focus');

Fiddle更新this works in full screen)。正如建议here

d。 setTimeout .trigger('focus')

setTimeout(function(){
   $("#test").trigger('focus');
},100);

Fiddle更新this works in full screen)。正如建议here

电子。普通的JavaScript

document.getElementById("test").focus();

Fiddle更新this works in full screen)。

F。使用纯JavaScript的setTimeout

setTimeout('document.getElementById("test").focus();',100)

Fiddle更新this works in full screen)。正如建议here

-G。 .select()

$("#test").select();

Fiddle更新this works in full screen)。正如建议here

小时。 setTimeout .select()

setTimeout(function(){
   $("#test").select();
},100);

Fiddle更新this works in full screen)。

这可能是Mozilla需要修复的问题(请参阅here),但似乎确实存在变通方法。例如,如果您在Mac上的Firefox中转到Google.com,请关注input字段(我无法弄清楚Google是如何做到这一点的,尽管他们似乎正在处理专注于this script)。

所以有人知道解决方法吗?

1 个答案:

答案 0 :(得分:0)

<input type="text" id="test" autofocus="autofocus">

如果将autofocus属性添加到输入中,它可能会执行您要执行的操作。

相关问题