是否可以在 JavaScript 中更改 document.activeElement?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3995524/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is it possible to change document.activeElement in JavaScript?
提问by mgamer
Is it possible to programmatically change the value of the document.activeElement
property in JavaScript?
是否可以以编程方式更改document.activeElement
JavaScript中的属性值?
回答by Tim Down
In IE, use the setActive()
method of the element that you want to be the active element. In other browsers that support activeElement
, you can use the focus()
method of the element, so long as the element is capable of receiving the focus (form elements, editable elements, elements with tabindex
set).
在 IE 中,使用setActive()
要成为活动元素的元素的方法。在其他支持 的浏览器中activeElement
,您可以使用focus()
元素的方法,只要该元素能够接收焦点(表单元素、可编辑元素、带有tabindex
集合的元素)。
If you want to set the activeElement back to the default (the <body>
element in most browsers), just call the active element's blur()
method:
如果要将 activeElement 设置回默认值(<body>
大多数浏览器中的元素),只需调用 active 元素的blur()
方法:
document.activeElement.blur();