Is tabindex hack for CSS :focus specified somewhere?

你离开我真会死。 提交于 2019-12-25 08:07:42

问题


There is a hack to make work :focus pseudoclass for div elements: adding to div tabindex. Like this:

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

Is this behavior specified somewhere in W3C documents (where?) or is it just a non-documented hack?

"This behavior" consists of:

  1. div element is not focusable by default.
  2. div element with tabindex is focusable.

回答1:


tabindex is one of the global attributes. This means it can be specified on all HTML elements.

0 is a valid value (see "If the value is a zero" under the definition of tabindex).

So your HTML is fine.




回答2:


tabindex will work on the following elements in 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


来源:https://stackoverflow.com/questions/41511512/is-tabindex-hack-for-css-focus-specified-somewhere

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!