How do you clear the focus in javascript?

会有一股神秘感。 提交于 2019-12-17 05:43:24

问题


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 element it is on without knowing ahead of time which element the focus is on. It has to work on firefox 2 as well as more modern browsers.

Is there a good way to do this?


回答1:


Answer: document.activeElement

---edit----

Technically: document.activeElement.blur()

----edit 2----

function onElementFocused(e)
{
    if (e && e.target)
        document.activeElement = e.target == document ? null : e.target;
} 

if (document.addEventListener) 
    document.addEventListener("focus", onElementFocused, true);



回答2:


.focus() and then .blur() something else arbitrary on your page. Since only one element can have the focus, it is transferred to that element and then removed.




回答3:


document.activeElement.blur();

Works wrong on IE9 - it blurs the whole browser window if active element is document body. Better to check for this case:

if (document.activeElement != document.body) document.activeElement.blur();



回答4:


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.




回答5:


dummyElem.focus() where dummyElem is a hidden object (e.g. has negative zIndex)?




回答6:


You can call window.focus();

but moving or losing the focus is bound to interfere with anyone using the tab key to get around the page.

you could listen for keycode 13, and forego the effect if the tab key is pressed.




回答7:


if you have a top menu with "active" class in a element, you can use it:

$(".menu-header:first").find(".active").focus();

it works for menu style like this:

<div class="menu-header" data-bind="invisible:isVisible">
                <ul class="nav navbar-nav navbar-header">
                    <li><a class="nv" href="/Home" id="home-menu"><span><i class="fa fa-home"></i></span>Hjem</a></li>
                    <li><a class="nv active" href="/nb-no/CV" id="cv-menu"><span><i class="fa fa-pencil-square-o"></i></span>CV</a></li>
                    <li><a class="nv" href="/myjobapps" id="app-menu"><span><i class="fa fa-files-o"></i></span>Søknader</a></li>
                    <li><a class="nv" href="/alladverts" id="jobs-menu"><span><i class="fa fa-search"></i></span>Jobber</a></li>
                    <li><a class="nv" href="/Options" id="option-menu"><span><i class="fa fa-ellipsis-h"></i></span>Mer</a></li>
                </ul>
            </div>



回答8:


With jQuery its just: $(this).blur();



来源:https://stackoverflow.com/questions/2520650/how-do-you-clear-the-focus-in-javascript

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!