gpt4 book ai didi

html - 如果元素被标签包裹,标签是否需要 "for"属性?

转载 作者:搜寻专家 更新时间:2023-10-31 08:03:51 25 4
gpt4 key购买 nike

假设我有一套 radio <input>秒。我不是穴居人,所以我知道我需要联想<label>与那些<input>秒。我喜欢将单选按钮包装在相应的标签中,例如 reasons enumerated here .

所以,例如:

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

此包装将相应的单选按钮与标签相关联。我需要定义标签的 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>

As pointed out通过 @Peter , “标签元素的 for 属性必须引用表单控件”(参见 http://www.w3.org/TR/html-markup/label.html ),但这可以理解为“如果指定可选的 for 属性,它必须引用有效的表单控件” .

最佳答案

根据 HTML5 规范 - “如果未指定 for 属性,但标签元素具有可标记元素后代,则树顺序中的第一个此类后代是标签元素的标签控件。”

http://www.w3.org/TR/html5/forms.html#category-label

所以基本上,不需要它,只要它包装这些元素中的任何一个:button、input(如果 type 属性不处于隐藏状态)、keygen、meter、output、progress、select 或 textarea

关于html - 如果元素被标签包裹,标签是否需要 "for"属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27826996/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com