--- id: 587d778b367417b2b2512aa7 title: 将单选按钮包裹在 fieldset 元素中以获得更好的可访问性 challengeType: 0 videoUrl: 'https://scrimba.com/c/cVJVefw' forumTopicId: 301030 dashedName: wrap-radio-buttons-in-a-fieldset-element-for-better-accessibility --- # --description-- 接下来的表单主题是关于单选按钮的可访问性。 在上一个挑战中,每个单选按钮都有一个包含 `for` 属性的 `label` 标签,这些属性值指向相关选项的 `id`。 然而单选按钮通常成组出现,而且用户必须选择其中一项。 `fieldset` 标签包裹整组单选按钮,实现这个功能。 它经常使用 `legend` 标签来提供分组的描述,以便屏幕阅读器用户会阅读 `fieldset` 元素中的每个选项。 当选项的含义很明确时,如“性别选择”,`fieldset` 与 `legend` 标签可以省略。 这时,使用包含 `for` 属性的 `label` 标签就足够了。 举个例子: ```html
Choose one of these three items:

``` # --instructions-- 当用户使用邮件注册时,Camper Cat 想知道他们的忍者等级。 他添加了一组单选按钮,并从我们之前的课程中吸取了教训,为每个选项使用带有 `for` 属性的 `label` 标签。 Camper Cat 加油! 然而,他仍然需要一些帮助。 请将包含单选按钮组的 `div` 标签替换为 `fieldset` 标签;将 `p` 标签替换为 `legend` 标签。 # --hints-- 应存在 1 个 `fieldset` 标签包含单选按钮组。 ```js assert($('fieldset').length == 1); ``` 确保 `fieldset` 标签是闭合的。 ```js assert( code.match(/<\/fieldset>/g) && code.match(/<\/fieldset>/g).length === code.match(/
/g).length ); ``` 应存在 1 个包含询问用户忍者等级内容文字的 `legend` 标签。 ```js assert($('legend').length == 1); ``` 不应存在 `div` 标签。 ```js assert($('div').length == 0); ``` 不应存在包含询问用户忍者等级内容文字的 `p` 标签。 ```js assert($('p').length == 4); ``` # --seed-- ## --seed-contents-- ```html

Deep Thoughts with Master Camper Cat

Sign up to receive Camper Cat's blog posts by email here!

What level ninja are you?



The Garfield Files: Lasagna as Training Fuel?

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...

Defeating your Foe: the Red Dot is Ours!

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...

Is Chuck Norris a Cat Person?

Chuck Norris is widely regarded as the premier martial artist on the planet, and it's a complete coincidence anyone who disagrees with this fact mysteriously disappears soon after. But the real question is, is he a cat person?...

``` # --solutions-- ```html

Deep Thoughts with Master Camper Cat

Sign up to receive Camper Cat's blog posts by email here!

What level ninja are you?

The Garfield Files: Lasagna as Training Fuel?

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...

Defeating your Foe: the Red Dot is Ours!

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...

Is Chuck Norris a Cat Person?

Chuck Norris is widely regarded as the premier martial artist on the planet, and it's a complete coincidence anyone who disagrees with this fact mysteriously disappears soon after. But the real question is, is he a cat person?...

```