Detect which form input has focus using JavaScript or jQuery

前端 未结 9 2557
灰色年华
灰色年华 2020-11-29 09:20

How do you detect which form input has focus using JavaScript or jQuery?

From within a function I want to be able to determine which form input has focus. I\'d like

9条回答
  •  长情又很酷
    2020-11-29 09:30

    Here's a solution for text/password/textarea (not sure if I forgot others that can get focus, but they could be easily added by modifying the if clauses... an improvement could be made on the design by putting the if's body in it's own function to determine suitable inputs that can get focus).

    Assuming that you can rely on the user sporting a browser that is not pre-historic (http://www.caniuse.com/#feat=dataset):

            
    

    For pre-historic browsers, you can use the uglier:

            
    

提交回复
热议问题