freeCodeCamp/curriculum/challenges/chinese/01-responsive-web-design/applied-accessibility/use-tabindex-to-specify-the...

3.3 KiB
Raw Blame History

id challengeType videoUrl forumTopicId title
587d7790367417b2b2512ab1 0 https://scrimba.com/c/cmzRRcb 301028 使用 tabindex 指定多个元素的键盘焦点顺序

Description

tabindex属性还可以指定标签的 tab 键顺序,将它的值设置为大于或等于 1 就可以实现这个功能。 tabindex属性值为 1 的标签将首先获得键盘焦点,然后焦点将按照指定的tabindex的值23 等)的顺序进行移动,直到回到默认的或tabindex值为 0 的标签上,如此循环。 需要注意的是,当按照这种方式设置 tab 键顺序时,将会覆盖默认的顺序(标签在文档流中出现的顺序)。这可能会令那些希望从页面顶部开始导航的用户感到困惑。这个技术在某些情况下可能是必要的,但是对可访问性而言,在应用时要十分小心。 举个例子: <div tabindex="1">I get keyboard focus, and I get it first!</div> <div tabindex="2">I get keyboard focus, and I get it second!</div>

Instructions

Camper Cat 在他的励志名言页面中有一个搜索区域,他打算使用 CSS 将这个区域定位在页面的右上角。Camper Cat 希望他的搜索input与提交input表单控件是 tab 键顺序的前两项。请为搜索input添加tabindex属性,其值为 1。为提交input添加tabindex属性,其值为 2。

Tests

tests:
  - text: '你应该为搜索<code>input</code>标签添加<code>tabindex</code>属性。'
    testString: assert($('#search').attr('tabindex'));
  - text: '你应该为提交<code>input</code>标签添加<code>tabindex</code>属性。'
    testString: assert($('#submit').attr('tabindex'));
  - text: '搜索<code>input</code>标签的<code>tabindex</code>属性值应该为 1。'
    testString: assert($('#search').attr('tabindex') == '1');
  - text: '提交<code>input</code>标签的<code>tabindex</code>属性值应该为 2。'
    testString: assert($('#submit').attr('tabindex') == '2');

Challenge Seed

<body>
  <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>&ldquo;There's no Theory of Evolution, just a list of creatures I've allowed to live.&rdquo;<br>
    - Chuck Norris</p>
  </blockquote>
  <blockquote>
    <p>&ldquo;Wise men say forgiveness is divine, but never pay full price for late pizza.&rdquo;<br>
    - TMNT</p>
  </blockquote>
  <footer>&copy; 2018 Camper Cat</footer>
</body>

Solution

// solution required