Javascript focus and select not working in FF

前端 未结 4 2046
傲寒
傲寒 2021-01-20 22:02

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

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

I looked around for this and found that you cou

4条回答
  •  悲哀的现实
    2021-01-20 22:39

    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.

提交回复
热议问题