How do you clear the focus in javascript?

前端 未结 7 1762
遥遥无期
遥遥无期 2020-11-28 04:45

I know this shouldn\'t be that hard, but I couldn\'t find the answer on Google.

I want to execute a piece of javascript that will clear the focus from whatever eleme

相关标签:
7条回答
  • 2020-11-28 05:20

    None of the answers provided here are completely correct when using TypeScript, as you may not know the kind of element that is selected.

    This would therefore be preferred:

    if (document.activeElement instanceof HTMLElement)
        document.activeElement.blur();
    

    I would furthermore discourage using the solution provided in the accepted answer, as the resulting blurring is not part of the official spec, and could break at any moment.

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