Input focus in firefox cannot duplicate the solution to the problem

  • 2020-03-30 03:23:08
  • OfStack

Met a very thorny problem, today is doing some validation of the input box, the business logic of the project is on my side when I selected a select a particular option, need to show an input box allows users to input, but if the user doesn't do anything, must be the pop-up alert box allows users to input, such as its input, the input hidden away my side again.

Therefore, the following problem arises: when the input box is displayed, it needs to get the focus automatically
 
document.getElementById('id').focus(); 

En, tried, the effect is very good, can not help but the heart is pleased, done!

The first time I showed the input, it worked, but then when I turned off the alert, the input couldn't get focus.

The problem is difficult, and unknown so, so, a small check on the Internet to answer the way of the gods, found the following a move, a test, as expected very good!

After alert, the original
 
document.getElementByIdx('id').focus(); 

Switch to
 
window.setTimeout(function () { document.getElementById('id').focus();}, 0); 

Cool, problem solved!

SetTimeout means to delay the execution of the previous expression after the specified time after the load, in this case, function after the delay of 0ms.

When I just solve this problem, still do not know why in firefox so can solve, if there is a god know, give a message, let late students learn, I am grateful!

Related articles: