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

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

提交回复
热议问题