document.activeelement returns body

ぐ巨炮叔叔 提交于 2019-11-27 14:34:37

问题


<input id="text1" tabindex="1" onblur="invokeFunc()"/>
<input id="text2" tabindex="2" onblur="invokeFunc()"/>

function invokeFunc(){
   // ajax request 
   alert(document.activeElement); 
   // this returns body element in firefox, safari and chrome.
}

i am trying set focus onblur on text boxes with proper tabindex set.

when i invoke javascript function onblur and try to get document.activeelement then it always return me body element instead of active element where focus is.


回答1:


Between leaving the old element and entering the new element the active element is indeed the document/body itself.

Demo: http://jsfiddle.net/u3uNP/




回答2:


To Solve this problem replace document.activeElement with this.

JsFiddle: https://jsfiddle.net/PseudoNinja/8oq0c1by/



来源:https://stackoverflow.com/questions/11299832/document-activeelement-returns-body

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