Detect which form input has focus using JavaScript or jQuery

前端 未结 9 2558
灰色年华
灰色年华 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

    Otherwise, you could use the onfocus and onblur events.

    something like:

    
    

    and then have something like this in your javascript

    if (txtfocus==1)
    {
    //Whatever code you want to run
    }
    
    if (txtfocus==0)
    {
    //Something else here
    }
    

    But that would just be my way of doing it, and it might not be extremely practical if you have, say 10 inputs :)

提交回复
热议问题