Detect which form input has focus using JavaScript or jQuery

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

    I am not sure if this is the most efficient way, but you could try:

    var selectedInput = null;
    $(function() {
        $('input, textarea, select').focus(function() {
            selectedInput = this;
        }).blur(function(){
            selectedInput = null;
        });
    });
    
    0 讨论(0)
  • 2020-11-29 09:46

    Try

    window.getSelection().getRangeAt(0).startContainer
    
    0 讨论(0)
  • 2020-11-29 09:47

    If all you want to do is change the CSS for a particular form field when it gets focus, you could use the CSS ":focus" selector. For compatibility with IE6 which doesn't support this, you could use the IE7 library.

    0 讨论(0)
提交回复
热议问题