The solution to the problem that the input focus cannot be repeatedly acquired under Firefox _javascript tips

Source: Internet
Author: User
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!

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.