CSS 的 tabindex hack :focus 在某处指定了吗?

Is tabindex hack for CSS :focus specified somewhere?

有一个 hack 可以为 div 元素制作 :focus 伪类:添加到 div tabindex。像这样:

.testFocus:focus{
    background: red;
}
<div class="testFocus" tabindex="0">awesomeDiv</div>

这种行为是在 W3C 文档的某处(在哪里?)指定的,还是只是一个没有记录的 hack?

"This behavior" 包括:

  1. div 元素默认不可聚焦。
  2. div 具有 tabindex 的元素可聚焦。

tabindex 将作用于 HTML5 中的以下元素。 https://www.w3.org/TR/html5/editing.html#sequential-focus-navigation-and-the-tabindex-attribute

  • a elements that have an href attribute
  • link elements that have an href attribute
  • button elements
  • input elements whose type attribute are not in the Hidden state
  • select elements
  • textarea elements

tabindexglobal attributes 之一。这意味着它可以在所有 HTML 元素上指定。

0 是有效值(参见 definition of tabindex 下的 "If the value is a zero")。

所以你的 HTML 没问题。