2018-10-10 18:03:03 -04:00
|
|
|
|
---
|
|
|
|
|
id: 587d7790367417b2b2512ab1
|
2020-12-16 00:37:30 -07:00
|
|
|
|
title: 使用 tabindex 指定多个元素的键盘焦点顺序
|
2018-10-10 18:03:03 -04:00
|
|
|
|
challengeType: 0
|
2020-02-11 17:06:41 +08:00
|
|
|
|
videoUrl: 'https://scrimba.com/c/cmzRRcb'
|
|
|
|
|
forumTopicId: 301028
|
2021-01-13 03:31:00 +01:00
|
|
|
|
dashedName: use-tabindex-to-specify-the-order-of-keyboard-focus-for-several-elements
|
2018-10-10 18:03:03 -04:00
|
|
|
|
---
|
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
# --description--
|
|
|
|
|
|
2021-02-06 04:42:36 +00:00
|
|
|
|
`tabindex` 属性还可以指定元素的 tab 键焦点顺序, 将它的值设置为大于等于 1 的整数,就可以实现这个功能。
|
2020-12-16 00:37:30 -07:00
|
|
|
|
|
2021-02-06 04:42:36 +00:00
|
|
|
|
给元素设置 `tabindex="1"`,键盘将首先把焦点放在这个元素上。 然后它按照指定的 `tabindex` 值(2、3 等等)顺序循环,再移动到默认值和 `tabindex="0"` 项目。
|
2020-12-16 00:37:30 -07:00
|
|
|
|
|
2021-02-06 04:42:36 +00:00
|
|
|
|
需要注意的是,使用这种方式设置 tab 键焦点顺序会覆盖默认顺序,其中默认顺序为标签在文档流中出现的顺序。 这可能会让那些希望从页面顶部开始导航的用户感到困惑。 使用 tabindex 在某些情况下是必要的,但在使用时要充分考虑到页面的可访问性。
|
2020-12-16 00:37:30 -07:00
|
|
|
|
|
2020-02-11 17:06:41 +08:00
|
|
|
|
举个例子:
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2021-03-20 09:47:30 -06:00
|
|
|
|
```html
|
|
|
|
|
<div tabindex="1">I get keyboard focus, and I get it first!</div>
|
|
|
|
|
```
|
2020-12-16 00:37:30 -07:00
|
|
|
|
|
2021-03-20 09:47:30 -06:00
|
|
|
|
```html
|
|
|
|
|
<div tabindex="2">I get keyboard focus, and I get it second!</div>
|
|
|
|
|
```
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
# --instructions--
|
|
|
|
|
|
2021-03-20 09:47:30 -06:00
|
|
|
|
Camper Cat 在他的励志名言页面中有一个搜索区域,他打算使用 CSS 把这个区域定位在页面的右上角。 Camper Cat 希望他的搜索(search)`input` 与提交(submit)`input` 表单控件是 tab 键焦点顺序的前两项。 请给 `search` `input` 添加 `tabindex` 属性,将属性值设置为 `1`;给 `submit` `input` 添加一个 `tabindex` 属性,将属性值设置为 `2`。
|
2020-12-16 00:37:30 -07:00
|
|
|
|
|
2021-04-30 00:43:46 +09:00
|
|
|
|
另一件需要注意的事情是,单击元素时,某些浏览器可能会将你置于 tab 键焦点顺序的中间位置。 页面上已添加一个元素,以确保你始终从 tab 键焦点顺序的开头开始。
|
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
# --hints--
|
|
|
|
|
|
2021-02-06 04:42:36 +00:00
|
|
|
|
应给 `search` `input` 标签添加一个 `tabindex` 属性。
|
2020-12-16 00:37:30 -07:00
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
assert($('#search').attr('tabindex'));
|
2018-10-10 18:03:03 -04:00
|
|
|
|
```
|
|
|
|
|
|
2021-04-30 00:43:46 +09:00
|
|
|
|
应给 `submit` `input` 标签添加一个 `tabindex` 属性。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
```js
|
|
|
|
|
assert($('#submit').attr('tabindex'));
|
|
|
|
|
```
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2021-02-06 04:42:36 +00:00
|
|
|
|
`search` `input` 标签的 `tabindex` 属性值应为 1。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
```js
|
|
|
|
|
assert($('#search').attr('tabindex') == '1');
|
|
|
|
|
```
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2021-02-06 04:42:36 +00:00
|
|
|
|
`submit` `input` 标签的 `tabindex` 属性值应为 2。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
```js
|
|
|
|
|
assert($('#submit').attr('tabindex') == '2');
|
2018-10-10 18:03:03 -04:00
|
|
|
|
```
|
2020-02-11 17:06:41 +08:00
|
|
|
|
|
2021-01-13 03:31:00 +01:00
|
|
|
|
# --seed--
|
|
|
|
|
|
|
|
|
|
## --seed-contents--
|
|
|
|
|
|
|
|
|
|
```html
|
|
|
|
|
<body>
|
2021-04-30 00:43:46 +09:00
|
|
|
|
<div tabindex="1" class="overlay"></div>
|
2021-01-13 03:31:00 +01:00
|
|
|
|
<header>
|
|
|
|
|
<h1>Even Deeper Thoughts with Master Camper Cat</h1>
|
|
|
|
|
<nav>
|
|
|
|
|
<ul>
|
|
|
|
|
<li><a href="">Home</a></li>
|
|
|
|
|
<li><a href="">Blog</a></li>
|
|
|
|
|
<li><a href="">Training</a></li>
|
|
|
|
|
</ul>
|
|
|
|
|
</nav>
|
|
|
|
|
</header>
|
|
|
|
|
<form>
|
|
|
|
|
<label for="search">Search:</label>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<input type="search" name="search" id="search">
|
|
|
|
|
<input type="submit" name="submit" value="Submit" id="submit">
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
</form>
|
|
|
|
|
<h2>Inspirational Quotes</h2>
|
|
|
|
|
<blockquote>
|
|
|
|
|
<p>“There's no Theory of Evolution, just a list of creatures I've allowed to live.”<br>
|
|
|
|
|
- Chuck Norris</p>
|
|
|
|
|
</blockquote>
|
|
|
|
|
<blockquote>
|
|
|
|
|
<p>“Wise men say forgiveness is divine, but never pay full price for late pizza.”<br>
|
|
|
|
|
- TMNT</p>
|
|
|
|
|
</blockquote>
|
|
|
|
|
<footer>© 2018 Camper Cat</footer>
|
|
|
|
|
</body>
|
2021-04-30 00:43:46 +09:00
|
|
|
|
<style>
|
|
|
|
|
body {
|
|
|
|
|
height: 100%;
|
|
|
|
|
margin: 0 !important;
|
|
|
|
|
padding: 8px;
|
|
|
|
|
}
|
|
|
|
|
.overlay {
|
|
|
|
|
margin: -8px;
|
|
|
|
|
position: absolute;
|
|
|
|
|
width: 100%;
|
|
|
|
|
height: 100%;
|
|
|
|
|
}
|
|
|
|
|
</style>
|
2021-01-13 03:31:00 +01:00
|
|
|
|
```
|
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
# --solutions--
|
|
|
|
|
|
2021-01-13 03:31:00 +01:00
|
|
|
|
```html
|
|
|
|
|
<body>
|
2021-04-30 00:43:46 +09:00
|
|
|
|
<div tabindex="1" class="overlay"></div>
|
2021-01-13 03:31:00 +01:00
|
|
|
|
<header>
|
|
|
|
|
<h1>Even Deeper Thoughts with Master Camper Cat</h1>
|
|
|
|
|
<nav>
|
|
|
|
|
<ul>
|
|
|
|
|
<li><a href="">Home</a></li>
|
|
|
|
|
<li><a href="">Blog</a></li>
|
|
|
|
|
<li><a href="">Training</a></li>
|
|
|
|
|
</ul>
|
|
|
|
|
</nav>
|
|
|
|
|
</header>
|
|
|
|
|
<form>
|
|
|
|
|
<label for="search">Search:</label>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<input tabindex="1" type="search" name="search" id="search">
|
|
|
|
|
<input tabindex="2" type="submit" name="submit" value="Submit" id="submit">
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
</form>
|
|
|
|
|
<h2>Inspirational Quotes</h2>
|
|
|
|
|
<blockquote>
|
|
|
|
|
<p>“There's no Theory of Evolution, just a list of creatures I've allowed to live.”<br>
|
|
|
|
|
- Chuck Norris</p>
|
|
|
|
|
</blockquote>
|
|
|
|
|
<blockquote>
|
|
|
|
|
<p>“Wise men say forgiveness is divine, but never pay full price for late pizza.”<br>
|
|
|
|
|
- TMNT</p>
|
|
|
|
|
</blockquote>
|
|
|
|
|
<footer>© 2018 Camper Cat</footer>
|
|
|
|
|
</body>
|
2021-04-30 00:43:46 +09:00
|
|
|
|
<style>
|
|
|
|
|
body {
|
|
|
|
|
height: 100%;
|
|
|
|
|
margin: 0 !important;
|
|
|
|
|
padding: 8px;
|
|
|
|
|
}
|
|
|
|
|
.overlay {
|
|
|
|
|
margin: -8px;
|
|
|
|
|
position: absolute;
|
|
|
|
|
width: 100%;
|
|
|
|
|
height: 100%;
|
|
|
|
|
}
|
|
|
|
|
</style>
|
2021-01-13 03:31:00 +01:00
|
|
|
|
```
|