在单选按钮上使用"label for"

133 html label section508 radio-button

在单选按钮上使用"label for"参数时,要符合508*,以下是否正确?

 <label for="button one"><input type="radio" name="group1" id="r1" value="1" /> button one</label> 
Run Code Online (Sandbox Code Playgroud)

或者是这个?

 <input type="radio" name="group1" id="r1" value="1" /><label for="button one"> button one</label>
Run Code Online (Sandbox Code Playgroud)

我问的原因是在第二个例子中,"label"只包含文本而不是实际的单选按钮.

*1973年"康复法案"第508条要求联邦机构为残疾人提供软件和网站可访问性.

Mar*_*c W 210

你几乎得到了它.它应该是这样的:

<input type="radio" name="group1" id="r1" value="1" />
<label for="r1"> button one</label>
Run Code Online (Sandbox Code Playgroud)

值in for应该是您标记的元素的id.

  • 嗯..你如何在两个单选按钮之间切换一个标签?你不能有两个相同的ID ......:/ (5认同)
  • 你回答当然是对的,但玛莎有正确的答案.两个Martha示例都是完全有效的HTML5.例如,如果你想让整个事物都在一个框架中,那么用css设计第二个就更容易了.如果你想让标签出现在其他地方,那么首先要做一个.但两者都可以.最好的祝福! (4认同)

Mar*_*tha 81

这两种结构都是有效且可访问的,但该for属性应该等于idinput元素的属性:

<input type="radio" ... id="r1" /><label for="r1">button text</label>
Run Code Online (Sandbox Code Playgroud)

要么

<label for="r1"><input type="radio" ... id="r1" />button text</label>
Run Code Online (Sandbox Code Playgroud)

for属性在第二个版本(包含输入的标签)中是可选的,但是IIRC有一些旧的浏览器没有使标签文本可以点击,除非你包含它.使用相邻的同级选择器,第一个版本(输入后的标签)更容易使用CSS进行样式化+:

input[type="radio"]:checked+label {font-weight:bold;}
Run Code Online (Sandbox Code Playgroud)

  • 没错,尽管在第二个示例中,不需要"for"属性. (9认同)
  • 我认为如果使用'for'属性,有些浏览器版本只会使按钮文本"可点击",即在标签内包装输入是不够的. (4认同)
  • @Kirkland - http://www.w3.org/TR/html401/interact/forms.html#h-17.9似乎表明第二种形式是有效的,但有几个消息来源表明支持可能不普遍.在任何情况下,最好提供`for`属性. (2认同)
  • 谁在9.5年后投票否决了这一点:您是否意识到那是多么荒谬? (2认同)