freeCodeCamp/curriculum/challenges/chinese/01-responsive-web-design/applied-accessibility/improve-form-field-accessib...

4.6 KiB

id title challengeType videoUrl forumTopicId dashedName
587d778a367417b2b2512aa6 使用 label 元素提高表单的可访问性 0 https://scrimba.com/c/cGJMMAN 301016 improve-form-field-accessibility-with-the-label-element

--description--

合理地使用语义化的 HTML 标签和属性可以提升页面可访问性。 在接下来的挑战中,你将会看到在表单中使用属性的场景。

label 标签的文本内容通常会是表单组件的名称或标签。 这些文本表明了组件的意义,也提升了表单的可访问性。 label 标签的 for 属性将标签与表单组件绑定;同时,屏幕阅读器也会读取 for 属性的属性值。

在 HTML 基础章节中,我们已经学习使用了单选按钮标签。 在那个挑战中,为了让标签可以在点击的时候也选中输入框,我们将单选按钮 input 标签嵌套在了 label 标签里面。 在本节课程中,我们介绍了另外一种实现这个功能的方法,那就是使用 for 属性。

for 的属性值必须与表单组件的 id 属性值相同。 举个例子:

<form>
  <label for="name">Name:</label>
  <input type="text" id="name" name="name">
</form>

--instructions--

Camper Cat 觉得他的博客文章会有很多人订阅,因此他想添加一个电子邮件注册表单。 请为表示电子邮件的 label 标签添加 for 属性,并将其属性值设置为与 input 标签的 id 属性值相同。

--hints--

label标签应该有一个非空的 for 属性。

assert($('label').attr('for'));

for 的属性值应与用于输入邮箱的 input 标签 id 属性值相同。

assert($('label').attr('for') == 'email');

--seed--

--seed-contents--

<body>
  <header>
    <h1>Deep Thoughts with Master Camper Cat</h1>
  </header>
  <section>
    <form>
      <p>Sign up to receive Camper Cat's blog posts by email here!</p>


      <label>Email:</label>
      <input type="text" id="email" name="email">


      <input type="submit" name="submit" value="Submit">
    </form>
  </section>
  <article>
    <h2>The Garfield Files: Lasagna as Training Fuel?</h2>
    <p>The internet is littered with varying opinions on nutritional paradigms, from catnip paleo to hairball cleanses. But let's turn our attention to an often overlooked fitness fuel, and examine the protein-carb-NOM trifecta that is lasagna...</p>
  </article>
  <img src="samuraiSwords.jpeg" alt="">
  <article>
    <h2>Defeating your Foe: the Red Dot is Ours!</h2>
    <p>Felines the world over have been waging war on the most persistent of foes. This red nemesis combines both cunning stealth and lightning speed. But chin up, fellow fighters, our time for victory may soon be near...</p>
  </article>
  <img src="samuraiSwords.jpeg" alt="">
  <article>
    <h2>Is Chuck Norris a Cat Person?</h2>
    <p>Chuck Norris is widely regarded as the premier martial artist on the planet, and it's a complete coincidence that anyone who disagrees with this fact mysteriously disappears soon after. But the real question is, is he a cat person?...</p>
  </article>
  <footer>&copy; 2018 Camper Cat</footer>
</body>

--solutions--

<body>
  <header>
    <h1>Deep Thoughts with Master Camper Cat</h1>
  </header>
  <section>
    <form>
      <p>Sign up to receive Camper Cat's blog posts by email here!</p>


      <label for="email">Email:</label>
      <input type="text" id="email" name="email">


      <input type="submit" name="submit" value="Submit">
    </form>
  </section>
  <article>
    <h2>The Garfield Files: Lasagna as Training Fuel?</h2>
    <p>The internet is littered with varying opinions on nutritional paradigms, from catnip paleo to hairball cleanses. But let's turn our attention to an often overlooked fitness fuel, and examine the protein-carb-NOM trifecta that is lasagna...</p>
  </article>
  <img src="samuraiSwords.jpeg" alt="">
  <article>
    <h2>Defeating your Foe: the Red Dot is Ours!</h2>
    <p>Felines the world over have been waging war on the most persistent of foes. This red nemesis combines both cunning stealth and lightning speed. But chin up, fellow fighters, our time for victory may soon be near...</p>
  </article>
  <img src="samuraiSwords.jpeg" alt="">
  <article>
    <h2>Is Chuck Norris a Cat Person?</h2>
    <p>Chuck Norris is widely regarded as the premier martial artist on the planet, and it's a complete coincidence that anyone who disagrees with this fact mysteriously disappears soon after. But the real question is, is he a cat person?...</p>
  </article>
  <footer>&copy; 2018 Camper Cat</footer>
</body>