【问题标题】:document.activeElement.matches(:focus) is false. why is this?document.activeElement.matches(:focus) 为假。为什么是这样?
【发布时间】:2020-06-29 14:18:35
【问题描述】:

我有一个李:

<li _ngcontent-c21="" class="accordion li" tabindex="0" aria-expanded="true">

我已经标签到了。 document.activeElement 实际上就是这个元素。然而,

document.activeElement.matches(':focus')
false

让事情变得更加奇怪:

document.activeElement.focus()
undefined
document.activeElement.matches(':focus')
false

这里发生了什么? (这是铬,顺便说一句)

【问题讨论】:

    标签: javascript css google-chrome dom pseudo-class


    【解决方案1】:

    如果这发生在控制台中,则控制台是焦点,而不是元素或页面;因此,只要控制台处于焦点状态,元素就无法匹配 :focus。这不是Element#matches() 的限制,而是:focus 工作方式的副作用——:focus CSS 样式规则的行为方式相同。

    如果您在控制台中设置超时并在超时到期之前重新聚焦页面,或者在页面内的 &lt;script&gt; 中调用这些函数,document.activeElement 应该与预期的 :focus 匹配。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-09-01
      • 1970-01-01
      • 2013-05-30
      • 1970-01-01
      • 1970-01-01
      • 2018-07-01
      • 2016-03-30
      相关资源
      最近更新 更多