Html CSS中的tabindex
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4971912/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
tabindex in CSS
提问by Boris Hamanov
Is it possible to control tabindex with CSS and if yes, which browsers support it and on which elements?
是否可以使用 CSS 控制 tabindex,如果是,哪些浏览器支持它以及哪些元素?
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:
我应该说,我的目标是在 div 上捕获 keydown 事件。我看到这个页面http://www.quirksmode.org/js/events/keys.html#测试键盘事件,它表明 keydown 触发除文档和正文或某种表单元素或链接以外的任何东西的唯一方法是在其上声明 tabindex 。但我在 W3C 网站上读到:
The following elements support the tabindex attribute: A, AREA, BUTTON, INPUT, OBJECT, SELECT, and TEXTAREA.
以下元素支持 tabindex 属性:A、AREA、BUTTON、INPUT、OBJECT、SELECT 和 TEXTAREA。
So I am a little confused, what to do in order to be standarts compliant and make my use case work?
所以我有点困惑,该怎么做才能符合标准并使我的用例工作?
EDIT2
编辑2
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.
我的整个用例是一个包含大量内容和人工滚动条的 div。我可以用鼠标事件滚动它,但到目前为止键盘没有运气。
回答by filip
Update 2017
2017 年更新
As pointed out by @Wallop in the comments, the nav-index
property was dropped from the specin 2015 due to "lack of implementation interest".
正如@Wallop 在评论中指出的那样,由于“缺乏实施兴趣” ,该nav-index
属性在 2015 年从规范中删除。
Take a look at the nav-index
property introduced by W3C in CSS3-UI.
看看nav-index
W3C 在CSS3-UI 中引入的属性。
This property has exactly the same behavior as a tabindex
and is applicable to any element.
此属性与 a 具有完全相同的行为,tabindex
并且适用于任何元素。
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'
'nav-index' 属性是一种指定顺序导航顺序(也称为“跳位顺序”)的输入方法中立方式。此属性是 HTML4/XHTML1 属性“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.
作为用例的最佳符合标准的解决方案,nav-index
到目前为止(截至 2012 年 6 月)仅由 Opera 解释,并且也被 W3C 标记为“有风险的功能”,因此可能随时被删除。
Alternative cross-browser solutions are:
替代的跨浏览器解决方案是:
non-standards-compliant: set the
tabindex
attribute on aDIV
. This will work in all common browsers.standards-compliant: replace
DIV
by an anchor element (A
) without ahref
attribute set, style it withdisplay: block
and add thetabindex
attribute.
不符合标准:
tabindex
在DIV
. 这将适用于所有常见的浏览器。符合标准:替换
DIV
为A
没有href
属性集的锚元素 ( ),设置样式display: block
并添加tabindex
属性。
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?
关于 BoltClock 的观点,我同意 Tab 顺序是非常合乎逻辑的(文本选择顺序和 Tab 顺序都与文档中元素的排列顺序密切相关)。另一方面,CSS 今天有更广泛的用途。它不仅可以操作文档(content
属性)的内容,还可以操作触发事件时和是否触发的行为:即使用pointer-events
,display
否则z-index
指针事件的顺序将改变。如果这些是非常基本的 CSS 属性,为什么您也不能影响 KeyBoardEvents?
回答by Noitidart
This is a bit old but now there are css options like -moz-user-focus. I'm sure there is a webkit equivalent.
这有点旧,但现在有像 -moz-user-focus 这样的 css 选项。我确定有一个 webkit 等价物。
https://developer.mozilla.org/en-US/docs/CSS/-moz-user-focus
https://developer.mozilla.org/en-US/docs/CSS/-moz-user-focus
user-focus
is the standard cross browser attribute.
user-focus
是标准的跨浏览器属性。