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

后端 未结 16 1258
悲&欢浪女
悲&欢浪女 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: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.

提交回复
热议问题