JavaScript的焦点和选择不工作在FF [英] Javascript focus and select not working in FF

查看:146
本文介绍了JavaScript的焦点和选择不工作在FF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用jQuery,以下不在FF中工作,但它是在IE中

Using jQuery, the following is not working in FF, but it is in IE

$(this).focus().select();

我四处寻找,发现可以使用超时来解决这个问题,但那是不是我想要做的事情,如果我能避免它。有没有人知道另一种方式做到这一点,并在FF工作?

I looked around for this and found that you could use a timeout to get around this, but that is not something I want to do if I can avoid it. Does anyone know another way to do this and have it work in FF?

Metropolis

Metropolis

推荐答案

我也碰到过这个。我相信setTimeout()解决方案是唯一可以在Firefox中使用的方法。这个问题与事件处理的顺序有关,如果我没有记错的话:当调用focus()方法时,IE立即改变控制焦点,但是Firefox通过向事件队列添加一个焦点事件来处理它,直到事件队列当前事件处理完成后。 setTimeout()技巧可以工作,因为它会将焦点更改事件之后的代码的其余部分添加到事件队列中,从而在代码继续处理之前将其解决。

I've run into this before as well. I believe that the setTimeout() solution is the only way this will work in Firefox. The issue has to do with order of events processing, if I remember correctly: IE immediately changes control focus when the focus() method is invoked, but Firefox handles it by adding a focus event to the event queue, which doesn't resolve until after the current event processing has completed. The setTimeout() trick works because it adds the remainder of your code to another event to the event queue after the focus change event, causing it to resolve before your code continues processing.

这篇关于JavaScript的焦点和选择不工作在FF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆