How do you clear the focus in javascript?

前端 未结 7 1806
遥遥无期
遥遥无期 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

    Answer: document.activeElement

    To do what you want, use document.activeElement.blur()

    If you need to support Firefox 2, you can also use this:

    function onElementFocused(e)
    {
        if (e && e.target)
            document.activeElement = e.target == document ? null : e.target;
    } 
    
    if (document.addEventListener) 
        document.addEventListener("focus", onElementFocused, true);
    

提交回复
热议问题