document.activeElement.matches(:focus)为假。为什么是这样? [英] document.activeElement.matches(:focus) is false. why is this?
问题描述
我有一个li:
<li _ngcontent-c21="" class="accordion li" tabindex="0" aria-expanded="true">
我选择了哪个。 document.activeElement实际上是此元素。但是,
Which I have tabbed to. document.activeElement is in fact this element. However,
document.activeElement.matches(':focus')
false
并使事情变得更加怪异:
and to make matters even slightly more bizzare:
document.activeElement.focus()
undefined
document.activeElement.matches(':focus')
false
这是怎么回事? (这是chrome,顺便说一句)
What is going on here? (this is chrome, btw)
推荐答案
如果在控制台中发生这种情况,则控制台将成为焦点,而不是元素或确实是页面;因此,只要控制台处于焦点位置,元素就不能匹配:focus
。这不是 Element#matches()
的限制,而是:focus
的工作方式的副作用-:focus
CSS样式规则的行为相同。
If this is happening in the console, the console is in focus, not the element or indeed the page; therefore, elements cannot match :focus
as long as the console is in focus. This is not a limitation of Element#matches()
, but a side effect of how :focus
works — :focus
CSS style rules behave the same way.
如果您在控制台中设置了超时并重新调整了页面的焦点,超时到期,或在页面内的< script>
中调用这些函数, document.activeElement
应该与:focus
如预期。
If you either set a timeout in the console and refocus the page before the timeout expires, or call these functions in a <script>
within the page, document.activeElement
should match :focus
as expected.
这篇关于document.activeElement.matches(:focus)为假。为什么是这样?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!