在JavaScript中,是否有可能以编程方式更改document.activeElement
属性的值?
在JavaScript中,是否有可能以编程方式更改document.activeElement
属性的值?
在 IE 中,请使用要成为活动元素的元素的 setActive()
方法。在其他支持 activeElement
的浏览器中,只要该元素能够接收焦点(表单元素、可编辑元素、具有设置了 tabindex
的元素),就可以使用该元素的 focus()
方法。
如果要将 activeElement 设置回默认状态(大多数浏览器中的 <body>
元素),只需调用活动元素的 blur()
方法即可:
document.activeElement.blur();
.focus()
方法聚焦到想要的元素,这样它就会成为新的 document.activeElement
。tabIndex
。 - writofmandamusactiveElement
是一个iframe,但在Chrome 84上无效。不过,.blur()
可以解决这个问题。 - user3064538
tabindex="-1"
,就可以用.focus()
进行聚焦。 - Darlan Alves