freeCodeCamp/curriculum/challenges/chinese/01-responsive-web-design/applied-accessibility/use-tabindex-to-add-keyboard-focus-to-an-element.md
Oliver Eyton-Williams ee1e8abd87
feat(curriculum): restore seed + solution to Chinese (#40683)
* feat(tools): add seed/solution restore script

* chore(curriculum): remove empty sections' markers

* chore(curriculum): add seed + solution to Chinese

* chore: remove old formatter

* fix: update getChallenges

parse translated challenges separately, without reference to the source

* chore(curriculum): add dashedName to English

* chore(curriculum): add dashedName to Chinese

* refactor: remove unused challenge property 'name'

* fix: relax dashedName requirement

* fix: stray tag

Remove stray `pre` tag from challenge file.

Signed-off-by: nhcarrigan <nhcarrigan@gmail.com>

Co-authored-by: nhcarrigan <nhcarrigan@gmail.com>
2021-01-12 19:31:00 -07:00

5.0 KiB
Raw Blame History

id, title, challengeType, videoUrl, forumTopicId, dashedName
id title challengeType videoUrl forumTopicId dashedName
587d7790367417b2b2512ab0 使用 tabindex 将键盘焦点添加到元素中 0 https://scrimba.com/c/cmzMDHW 301027 use-tabindex-to-add-keyboard-focus-to-an-element

--description--

HTML 的 tabindex 属性是与标签焦点相关的属性,给标签加上 tabindex 表示该标签可以获得焦点。tabindex 的值可以是零、负整数或正整数,这些数值分别定义了三种行为。

当用户在页面中使用 tab 键时,有些标签(如:链接、表单控件)可以自动获得焦点,它们获得焦点的顺序与它们出现在文档流中的顺序一致。我们可以通过将其他标签(如 divspanp 等)的 tabindex 属性值设为 0 来让它们实现类似的效果。比如:

<div tabindex="0">I need keyboard focus!</div>

注意:
tabindex 属性值为负整数(通常为 -1的标签也是可以获得焦点的只是不可以通过键盘操作如 tab 键)来获得焦点。这种方法通常用于以编程的方式使内容获得焦点(如:将焦点设置到用 div 实现的弹出框上)的场景。只是这部分内容已经超出了当前挑战的范围。

--instructions--

Camper Cat 新建了一个用来收集他的用户信息的调查。他知道输入框可以自动获得键盘焦点但他希望用户使用键盘切换标签时焦点可以停留在指示文字Instructions上。请给 p 标签添加 tabindex 属性,并将它的属性值设为 0。注意使用 tabindex 属性还可以让 CSS 伪类 :focusp 标签上生效。

--hints--

表单中作为指示文字Instructionsp 标签应具有 tabindex 属性。

assert($('p').attr('tabindex'));

p 标签的 tabindex 属性值应设置为 0。

assert($('p').attr('tabindex') == '0');

--seed--

--seed-contents--

<head>
  <style>
  p:focus {
    background-color: yellow;
  }
  </style>
</head>
<body>
  <header>
    <h1>Ninja Survey</h1>
  </header>
  <section>
    <form>


      <p>Instructions: Fill in ALL your information then click <b>Submit</b></p>


      <label for="username">Username:</label>
      <input type="text" id="username" name="username"><br>
      <fieldset>
        <legend>What level ninja are you?</legend>
        <input id="newbie" type="radio" name="levels" value="newbie">
        <label for="newbie">Newbie Kitten</label><br>
        <input id="intermediate" type="radio" name="levels" value="intermediate">
        <label for="intermediate">Developing Student</label><br>
        <input id="master" type="radio" name="levels" value="master">
        <label for="master">9th Life Master</label>
      </fieldset>
      <br>
      <fieldset>
      <legend>Select your favorite weapons:</legend>
      <input id="stars" type="checkbox" name="weapons" value="stars">
      <label for="stars">Throwing Stars</label><br>
      <input id="nunchucks" type="checkbox" name="weapons" value="nunchucks">
      <label for="nunchucks">Nunchucks</label><br>
      <input id="sai" type="checkbox" name="weapons" value="sai">
      <label for="sai">Sai Set</label><br>
      <input id="sword" type="checkbox" name="weapons" value="sword">
      <label for="sword">Sword</label>
      </fieldset>
      <br>
      <input type="submit" name="submit" value="Submit">
    </form><br>
  </section>
  <footer>&copy; 2018 Camper Cat</footer>
</body>

--solutions--

<head>
  <style>
  p:focus {
    background-color: yellow;
  }
  </style>
</head>
<body>
  <header>
    <h1>Ninja Survey</h1>
  </header>
  <section>
    <form>


      <p tabindex="0">Instructions: Fill in ALL your information then click <b>Submit</b></p>


      <label for="username">Username:</label>
      <input type="text" id="username" name="username"><br>
      <fieldset>
        <legend>What level ninja are you?</legend>
        <input id="newbie" type="radio" name="levels" value="newbie">
        <label for="newbie">Newbie Kitten</label><br>
        <input id="intermediate" type="radio" name="levels" value="intermediate">
        <label for="intermediate">Developing Student</label><br>
        <input id="master" type="radio" name="levels" value="master">
        <label for="master">9th Life Master</label>
      </fieldset>
      <br>
      <fieldset>
      <legend>Select your favorite weapons:</legend>
      <input id="stars" type="checkbox" name="weapons" value="stars">
      <label for="stars">Throwing Stars</label><br>
      <input id="nunchucks" type="checkbox" name="weapons" value="nunchucks">
      <label for="nunchucks">Nunchucks</label><br>
      <input id="sai" type="checkbox" name="weapons" value="sai">
      <label for="sai">Sai Set</label><br>
      <input id="sword" type="checkbox" name="weapons" value="sword">
      <label for="sword">Sword</label>
      </fieldset>
      <br>
      <input type="submit" name="submit" value="Submit">
    </form><br>
  </section>
  <footer>&copy; 2018 Camper Cat</footer>
</body>