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

后端 未结 10 1268
北恋
北恋 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:58

    $('input:focus')

    It's CSS. You don't need to create a "custom selector." It already exists! http://www.w3schools.com/CSS/pr_pseudo_focus.asp

    Just attach whatever process you want to do to that selector, and it will weed it out if the element in question is not focused. I did this recently to keep a keyup from instantiating an email input error check when the e-mail input wasn't being used.

    If all you're trying to do is check if the user has focused on anything themselves, just do this:

    if($('input:focus').size() == 0){
        /* Perform your function! */
    }
    

提交回复
热议问题