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!