After the blur of jquery, Focus does not get the focus of the solution

Source: Internet
Author: User

A: Many children's shoes may encounter this situation: after the blur of jquery, focus is not focused.

Second: The use of the scene:

When the user fills in the information, if the mailbox is empty, then prompts to fill in the mailbox, and puts the cursor in fills in the mailbox the text box, facilitates the user to enter again.

Take it for granted we use Jquey's blur () and focus () two functions to achieve the above requirements:
$ ("#email"). blur (function() {       if($ ("#email"). val () = = ') {            alert (' Please fill in your email address ');            $ ("#email"). focus ();           }    })
Three: Code interpretation: When the text box to fill in the email loses focus, if the filled out the mailbox is empty, then pop ' please fill in your mailbox ' prompt, and regain the focus of the text box, convenient for users to re-enter. After running test.html on FF: you will find that the frame is bouncing out, but it is not getting the focus, it is very strange, the code is obviously not wrong ah, on IE, the results of normal operation, both pop-up information, but also get the focus. Four: Then you must have thought, this is again a compatibility problem. Yes. The compatibility of FF and IE has caused us a lot of trouble. V: The cause of the problem should be that the FF and IE's mechanisms for blur and focus are not the same.

The focus of FF is only before Blur.

Six: So how to solve this bug? Use settimeout to do a delay.

Modify the code as follows:

$ ("#email"). blur (function() {    if($ ("#email"). val () = = ') {        alert (' Please fill in your email address ')        ; // $ ("#email"). focus ();        Window.settimeout (function() {document.getElementById (' email '). Select ();},0 );         }    
Seven: Run on IE and FF respectively, perfect compatible.

After the blur of jquery, Focus does not get the focus of the solution

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.