How do I find out which DOM element has the focus?

后端 未结 16 1260
悲&欢浪女
悲&欢浪女 2020-11-22 03:12

I would like to find out, in JavaScript, which element currently has focus. I\'ve been looking through the DOM and haven\'t found what I need, yet. Is there a way to do this

相关标签:
16条回答
  • 2020-11-22 03:46

    If you want to get a object that is instance of Element, you must use document.activeElement, but if you want to get a object that is instance of Text, you must to use document.getSelection().focusNode.

    I hope helps.

    0 讨论(0)
  • 2020-11-22 03:47

    Reading other answers, and trying myself, it seems document.activeElement will give you the element you need in most browsers.

    If you have a browser that doesn't support document.activeElement if you have jQuery around, you should be able populate it on all focus events with something very simple like this (untested as I don't have a browser meeting those criteria to hand):

    if (typeof document.activeElement === 'undefined') { // Check browser doesn't do it anyway
      $('*').live('focus', function () { // Attach to all focus events using .live()
        document.activeElement = this; // Set activeElement to the element that has been focussed
      });
    }
    
    0 讨论(0)
  • 2020-11-22 03:47

    Just putting this here to give the solution I eventually came up with.

    I created a property called document.activeInputArea, and used jQuery's HotKeys addon to trap keyboard events for arrow keys, tab and enter, and I created an event handler for clicking into input elements.

    Then I adjusted the activeInputArea every time focus changed, so I could use that property to find out where I was.

    It's easy to screw this up though, because if you have a bug in the system and focus isn't where you think it is, then its very hard to restore the correct focus.

    0 讨论(0)
  • 2020-11-22 03:50

    JQuery does support the :focus pseudo-class as of current. If you are looking for it in the JQuery documentation, check under "Selectors" where it points you to the W3C CSS docs. I've tested with Chrome, FF, and IE 7+. Note that for it to work in IE, <!DOCTYPE... must exist on the html page. Here is an example assuming you've assigned an id to the element that has focus:

    $(":focus").each(function() {
      alert($(this).attr("id") + " has focus!");
    });
    
    0 讨论(0)
  • 2020-11-22 03:51

    I have found the following snippet to be useful when trying to determine which element currently has focus. Copy the following into the console of your browser, and every second it will print out the details of the current element that has focus.

    setInterval(function() { console.log(document.querySelector(":focus")); }, 1000);
    

    Feel free to modify the console.log to log out something different to help you pinpoint the exact element if printing out the whole element does not help you pinpoint the element.

    0 讨论(0)
  • 2020-11-22 03:52

    As said by JW, you can't find the current focused element, at least in a browser-independent way. But if your app is IE only (some are...), you can find it the following way :

    document.activeElement
    

    EDIT: It looks like IE did not have everything wrong after all, this is part of HTML5 draft and seems to be supported by the latest version of Chrome, Safari and Firefox at least.

    0 讨论(0)
提交回复
热议问题