如果元素被标签包装,标签是否需要“for”属性?(If an element is wrapped by a label, does the label require the “for” attribute?)
假设我有一组收音机<input> 。 我不是一个穴居人,所以我知道我需要将<label>与那些<input> 。 由于这里列举的原因 ,我喜欢将单选按钮包裹在相应的标签中。
所以,例如:
<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" />ah, that would be redundant and repetitive</label> </fieldset>这个包装将相应的单选按钮与标签相关联。 我是否也需要定义标签的属性?
<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" />ah, that would be redundant and repetitive</label> </fieldset>正如 @Peter 指出的那样 ,“标签元素的for属性必须引用一个表单控件”(见http://www./TR/html-markup/label.html ),但这可以被读取意思是“如果你指定了可选for属性,它必须引用一个有效的表单控件”。
Say I have a set of radio <input>s. I'm not a caveman, so I know I need to associate <label> with those <input>s. I'm fond of wrapping the radio butt within their corresponding labels, for reas enumerated here.
So, for example:
<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" />ah, that would be redundant and repetitive</label> </fieldset>This wrapping associates the corresponding radio button with the label. Do I also need to define the label's for attribute?
<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" />ah, that would be redundant and repetitive</label> </fieldset>As pointed out by @Peter, "The for attribute of the label element must refer to a form control" (see http://www./TR/html-markup/label.html), but this could be read to mean "if you specify the optional for attribute, it must refer to a valid form control".
最满意答案
根据HTML5规范 - “如果没有指定for属性,但标签元素具有可描述的元素后代,那么第一个树形顺序的后代是标签元素的标签控件。”
http://www./TR/html5/forms.html#category-label
所以基本上不需要,只要它包装任何这些元素:按钮,输入(如果type属性不处于隐藏状态),keygen,仪表,输出,进度,选择或textarea
According to the HTML5 spec - "If the for attribute is not specified, but the label element has a labelable element descendant, then the first such descendant in tree order is the label element's labeled control."
http://www./TR/html5/forms.html#category-label
So basically, no it is not required as long as it is wrapping any of these elements: button, input (if the type attribute is not in the hidden state), keygen, meter, output, progress, select, or textarea
#感谢您对电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格的认可,转载请说明来源于"电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格
推荐阅读
留言与评论(共有 8 条评论) |
本站网友 眼部注射除皱方法 | 2分钟前 发表 |
我是否也需要定义标签的属性? <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 | |
本站网友 马鞍山二手 | 6分钟前 发表 |
//www./TR/html5/forms.html#category-label So basically | |
本站网友 后背体位 | 13分钟前 发表 |
所以我知道我需要将<label>与那些<input> | |
本站网友 5级地震 | 2分钟前 发表 |
//www./TR/html5/forms.html#category-label So basically | |
本站网友 芳芳菲菲 | 2分钟前 发表 |
进度 | |
本站网友 翁卫军 | 5分钟前 发表 |
for reas enumerated here. So | |
本站网友 天津万科魅力之城 | 17分钟前 发表 |
//www./TR/html-markup/label.html) |