The input focus of Firefox cannot be retrieved repeatedly. _ javascript tips-js tutorial

Source: Internet
Author: User
When the input box is displayed, You need to automatically obtain the focus, which can be easily done with focus, but input cannot obtain the focus in Firefox, I would like to share with you the following good solution. Today I encountered a very difficult problem, that is, when I did some input box verification, the business logic of my project here is that when I select a specific option of a select statement, I need to display an input box for the user to input something, but if the user does not do anything, A warning box must be displayed to allow the user to enter the information. After the user enters the information, I will hide the input.

Therefore, the following problem occurs: when the input box is displayed, the focus needs to be automatically obtained. At this time, I use

The Code is as follows:


Document. getElementById ('id'). focus ();


Well, I tried it and it worked very well!

However, the sky is not as good as human computing. When I used Firefox for testing, it was finished. The first time I showed the input, it was easy to use. However, when I closed the alert prompt box, the input cannot obtain the focus. Fail!

The problem is tricky and unknown. so, I checked the answers from the online experts and found the following trick. I tried it and it worked very well!

After alert

The Code is as follows:


Document. getElementByIdx ('id'). focus ();


Change

The Code is as follows:


Window. setTimeout (function () {document. getElementById ('id'). focus () ;}, 0 );


Great, problem solving!

SetTimeout refers to the expression before execution after the specified time delay after loading. Of course, the function is executed after the delay of 0 ms.

When I solve this problem, I still don't know why I can solve it in Firefox. If any great God knows, I 'd like to leave a message so that I can learn it later. I am very grateful!
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.