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!