标记无线电和复选框元素

时间:2010-09-22 17:41:03

标签: html xhtml semantics

标记复选框和无线电元素的最合适,语义正确的方法是什么?显然,给每个人一个唯一的id属性是一个选项,并在id <label for="">中使用它,但这似乎会破坏语义分组。将未封闭的文本放在input元素旁边似乎是错误的。

编辑: 另外,如何表示一组这样的元素的标签? (即不是每个选项的描述,而是整个组的描述)

2 个答案:

答案 0 :(得分:3)

使用<label for="">是正确的方法。单选按钮的分组是通过名称属性表示组,通过id属性表示单个元素。例如:

<input type="radio" name="rbg" id="rbg1"><label for="rbg1">Button One</label><br>
<input type="radio" name="rbg" id="rbg2"><label for="rbg2">Button Two</label><br>
<input type="radio" name="rbg" id="rbg3"><label for="rbg3">Button Three</label><br>

至于将标签放在整个组上,只需将它们放在容器中,该容器在按钮(或复选框)堆栈上方有一个文本容器。例如:

<div>
  <h3>My Radio Button Group</h3>
  <input type="radio" name="rbg" id="rbg1"><label for="rbg1">Button One</label><br>
  <input type="radio" name="rbg" id="rbg2"><label for="rbg2">Button Two</label><br>
  <input type="radio" name="rbg" id="rbg3"><label for="rbg3">Button Three</label><br>
</div>

答案 1 :(得分:1)

我主要使用Chris Coyiers HTML ipsum:http://html-ipsum.com/

总是一个好帮手

相关问题