Today I have a very difficult problem, is to do some input box validation, my side of the project business logic is when I select a Select a specific option, you need to display an input box to allow users to input things, but if the user does not do anything, You must eject the warning box to let the user input, and so on, I will hide the input.
Therefore, the following problems arise, input input box to show the need to automatically get focus, at this time, I am using the
Copy Code code as follows:
document.getElementById (' id '). focus ();
Well, try it, the effect is very good, can't help heart exultation, fix!
But the sky is not as good as people, in my run with the Firefox test, finished, the first time the input is shown to be useful, but then I close alert box, input can not get focus, embarrassed!
The problem is difficult, but also unknown to it, so, a small check the online great god of the solution, found the following a recruit, tested a bit, really good!
After alert, the original
Copy Code code as follows:
DOCUMENT.GETELEMENTBYIDX (' id '). focus ();
Into
Copy Code code as follows:
Window.settimeout (function () {document.getElementById (' id '). focus ();}, 0;
Cool, solve the problem!
SetTimeout refers to the execution of the preceding expression after a specified time delay after loading, and of course the function is executed after the delay of 0ms.
When I just solved the problem, still do not know why in Firefox under this can be resolved, such as the Great God know, give a message, let's learn a little bit, appreciate!