假设我有一台收音机<input>
s。我不是穴居人,所以我知道我需要交往<label>
和那些<input>
s。我喜欢将单选按钮包装在相应的标签中,例如这里列举的原因.
因此,例如:
<fieldset>
<legend>Should I provide a "for" attribute?</legend>
<label><input type="radio" name="define_the_for_attribute" id="define_the_for_attribute_yes" value="yes" />Yep, if you know what's good for you</label>
<label><input type="radio" name="define_the_for_attribute" id="define_the_for_attribute_no" value="no" />Nah, that would be redundant and repetitive</label>
</fieldset>
此包装将相应的单选按钮与标签相关联。我是吗also需要定义标签的for
属性?
<fieldset>
<legend>Should I provide a "for" attribute?</legend>
<label for="define_the_for_attribute_yes"><input type="radio" name="define_the_for_attribute" id="define_the_for_attribute_yes" value="yes" />Yep, if you know what's good for you</label>
<label for="define_the_for_attribute_no"><input type="radio" name="define_the_for_attribute" id="define_the_for_attribute_no" value="no" />Nah, that would be redundant and repetitive</label>
</fieldset>
正如所指出的 by @Peter, “label 元素的 for 属性必须引用表单控件”(参见http://www.w3.org/TR/html-markup/label.html),但这可以理解为“如果您指定可选的for
属性,它必须引用有效的表单控件”。
根据 HTML5 规范 - “如果未指定 for 属性,但 label 元素具有可标记元素后代,则树顺序中的第一个此类后代是 label 元素的标记控件。”
http://www.w3.org/TR/html5/forms.html#category-label
所以基本上,不,只要它包装以下任何元素就不是必需的:按钮、输入(如果类型属性不处于隐藏状态)、keygen、meter、output、progress、select 或 textarea
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)