tabindex in CSS

喜欢而已 提交于 2019-11-27 13:25:12

问题


Is it possible to control tabindex with CSS and if yes, which browsers support it and on which elements?

EDIT

I should say, my goal is to catch keydown events on a div. I saw this page http://www.quirksmode.org/js/events/keys.html# that tests keyboard events and it shows that the only way keydown fires on anything other than document and body or some kind of form element or link is to have tabindex declared on it. But I read on W3C site:

The following elements support the tabindex attribute: A, AREA, BUTTON, INPUT, OBJECT, SELECT, and TEXTAREA.

So I am a little confused, what to do in order to be standarts compliant and make my use case work?

EDIT2

My whole use case is a div with a lot of content with an artificial scroll bar. I am able to scroll it with mouse events but no luck with the keyboard so far.


回答1:


Update 2017

As pointed out by @Wallop in the comments, the nav-index property was dropped from the spec in 2015 due to "lack of implementation interest".


Take a look at the nav-index property introduced by W3C in CSS3-UI.

This property has exactly the same behavior as a tabindex and is applicable to any element.

The ‘nav-index’ property is an input-method-neutral way of specifying the sequential navigation order (also known as "tabbing order"). This property is a replacement for the HTML4/XHTML1 attribute ‘tabindex’

Being probably the best standards-compliant solution for the use case, nav-index is interpreted only by Opera so far (as of June 2012) and is also marked as "Feature at risk" by W3C, therefore may be dropped any time.

Alternative cross-browser solutions are:

  • non-standards-compliant: set the tabindex attribute on a DIV. This will work in all common browsers.

  • standards-compliant: replace DIV by an anchor element (A) without a href attribute set, style it with display: block and add the tabindex attribute.

With respect to BoltClock´s point, I agree that the tabbing order is very logical (both the text selection order and tabbing order are closely related to the sequence in which elements are aranged in the document). On the other hand, CSS has a wider purpose today. It can manipulate not just the content of a document (content property) but also the behavior when and if events are to be fired: i.e. using pointer-events, display or z-index the pointer event's order will change. If these are very basic CSS properties, why you should not be able to influence KeyBoardEvents, too?




回答2:


This is a bit old but now there are css options like -moz-user-focus. I'm sure there is a webkit equivalent.

https://developer.mozilla.org/en-US/docs/CSS/-moz-user-focus

user-focus is the standard cross browser attribute.



来源:https://stackoverflow.com/questions/4971912/tabindex-in-css

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