Is there a 'has focus' in JavaScript (or jQuery)?

后端 未结 10 1269
北恋
北恋 2020-11-30 00:36

Is there something I can do like this (perhap via a plugin)

if ( ! $(\'form#contact input]\').hasFocus()) {
  $(\'form#contact input:first]\').focus();
}
         


        
10条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-30 00:51

    Frustratingly difficult to find a solution to this problem considering the solution is actually very simple:

    if (document.activeElement == this) {
      // has focus
    }
    
    if (document.activeElement != this) {
      // does not have focus
    }

提交回复
热议问题