--- id: 587d7790367417b2b2512ab1 title: 使用 tabindex 指定多个元素的键盘焦点顺序 challengeType: 0 videoUrl: 'https://scrimba.com/c/cmzRRcb' forumTopicId: 301028 dashedName: use-tabindex-to-specify-the-order-of-keyboard-focus-for-several-elements --- # --description-- `tabindex` 属性还可以指定元素的 tab 键焦点顺序, 将它的值设置为大于等于 1 的整数,就可以实现这个功能。 给元素设置 `tabindex="1"`,键盘将首先把焦点放在这个元素上。 然后它按照指定的 `tabindex` 值(2、3 等等)顺序循环,再移动到默认值和 `tabindex="0"` 项目。 需要注意的是,使用这种方式设置 tab 键焦点顺序会覆盖默认顺序,其中默认顺序为标签在文档流中出现的顺序。 这可能会让那些希望从页面顶部开始导航的用户感到困惑。 使用 tabindex 在某些情况下是必要的,但在使用时要充分考虑到页面的可访问性。 举个例子: ```html
“There's no Theory of Evolution, just a list of creatures I've allowed to live.”
- Chuck Norris
``` # --solutions-- ```html“Wise men say forgiveness is divine, but never pay full price for late pizza.”
- TMNT
“There's no Theory of Evolution, just a list of creatures I've allowed to live.”
- Chuck Norris
```“Wise men say forgiveness is divine, but never pay full price for late pizza.”
- TMNT