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

后端 未结 10 1109
北恋
北恋 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
    }

    0 讨论(0)
  • 2020-11-30 00:52

    There is no native solution but yes there is a more elegant way you can do it:

    jQuery.extend(jQuery.expr[':'], {
      focus: "a == document.activeElement"
    });
    

    You're defining a new selector. See Plugins/Authoring. Then you can do:

    if ($("...").is(":focus")) {
      ...
    }
    

    or:

    $("input:focus").doStuff();
    
    0 讨论(0)
  • 2020-11-30 00:52

    There is a plugin http://plugins.jquery.com/project/focused

    Also you can check Using jQuery to test if an input has focus

    0 讨论(0)
  • 2020-11-30 00:56

    I know this is an old question, but may be my solution will help someone :)

    since this didnt worked for me:

    if ($(this)!=$(document.activeElement)) { ... }
    

    ..were "this" is returned from blur function. So i did this:

    if ($(document.activeElement).attr("class") != "input_textbox"){ ... }
    
    0 讨论(0)
  • 2020-11-30 00:57

    I had trouble with cletus approach, using jQuery 1.3.2 and Firefox 3.6.8, because the string "a == document.activeElement" was not a valid function.

    I fixed it defining a function for the focus key. In fact, all other keys defined in jQuery.expr[':'] are defined as functions. Here's the code:

    jQuery.extend(jQuery.expr[':'], {
        focus: function(e){ return e == document.activeElement; }
    });
    

    So, now it works as expected.

    However, I was experiencing some strange behaviour in Firefox 3.6.8 (maybe a bug in FF?). If I clicked on an input text while the page was rendering, and if I called is(":focus") on page load, I would get an error from the browser, reported by FireBug, and the script would break.

    To solve this, I surrounded the code with a try...catch block, returning false on error. Use it if you want to prevent your users from experiencing the same error:

    jQuery.extend(jQuery.expr[':'], {
        focus: function(e){
            try{ return e == document.activeElement; }
            catch(err){ return false; }
        }
    });
    
    0 讨论(0)
  • 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! */
    }
    
    0 讨论(0)
提交回复
热议问题