Using jQuery to test if an input has focus

后端 未结 15 1418
梦谈多话
梦谈多话 2020-11-22 05:42

On the front page of a site I am building, several

s use the CSS :hover pseudo-class to add a border when the mouse is over them. One of
15条回答
  •  余生分开走
    2020-11-22 06:15

    April 2015 Update

    Since this question has been around a while, and some new conventions have come into play, I feel that I should mention the .live method has been depreciated.

    In its place, the .on method has now been introduced.

    Their documentation is quite useful in explaining how it works;

    The .on() method attaches event handlers to the currently selected set of elements in the jQuery object. As of jQuery 1.7, the .on() method provides all functionality required for attaching event handlers. For help in converting from older jQuery event methods, see .bind(), .delegate(), and .live().

    So, in order for you to target the 'input focused' event, you can use this in a script. Something like:

    $('input').on("focus", function(){
       //do some stuff
    });
    

    This is quite robust and even allows you to use the TAB key as well.

提交回复
热议问题