JavaScript FocusOut - get the element that receives focus

后端 未结 4 778
青春惊慌失措
青春惊慌失措 2021-02-13 13:03

When the FocusOut event is raised, how do you know which element receives the focus?

The correct way seems to be to use the event\'s relatedTarget p

4条回答
  •  后悔当初
    2021-02-13 13:24

    I have a hypothesis and workaround for firefox. document.activeElement seems to work. Then focusout hits, so it gets removed. By the time focusin hits (or maybe immediately after) there will be a focused element again. But between out and in there is nothing focused, so no element is being reported as active.

    My workaround is a stupid setTimeout hack. setTimeout( function() {console.log(document.activeElement)}, 1); reliably gets me an active element. Granted I've only tested in one machine and spent all of 90 seconds doing so, but it's the best I've found so far.

提交回复
热议问题