Using jQuery to test if an input has focus

后端 未结 15 1172
梦谈多话
梦谈多话 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:28

    Here’s a more robust answer than the currently accepted one:

    jQuery.expr[':'].focus = function(elem) {
      return elem === document.activeElement && (elem.type || elem.href);
    };
    

    Note that the (elem.type || elem.href) test was added to filter out false positives like body. This way, we make sure to filter out all elements except form controls and hyperlinks.

    (Taken from this gist by Ben Alman.)

提交回复
热议问题