Solution to the problem that the input focus cannot be retrieved repeatedly under Firefox

Source: Internet
Author: User

Today, I encountered a very difficult problem, that is, when some input boxes are verified, the business logic of my project is that when I select a specific option of a select, you need to display an input box for the user to input things. If the user does not do anything, a warning box must be displayed to allow the user to input things. After the user inputs them, I will hide the input here.

Therefore, the following problem occurs: when the input box is displayed, the focus needs to be automatically obtained. At this time, I use
Copy codeThe 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
Copy codeThe Code is as follows:
Document. getElementByIdx ('id'). focus ();

Change
Copy codeThe 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!

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.