【发布时间】:2011-09-24 01:19:28
【问题描述】:
标签和字段很简单;我们有<label>,然后是相关的输入字段。但是,对于该字段下的较小信息文本,最语义正确的 HTML 元素是什么?
【问题讨论】:
标签和字段很简单;我们有<label>,然后是相关的输入字段。但是,对于该字段下的较小信息文本,最语义正确的 HTML 元素是什么?
【问题讨论】:
我不知道连接它们的特定元素或属性,但您可以使用 ARIA 属性aria-describedby:
<label for="firstname">First Name</label>
<input type="text" id="firstname" aria-describedby="firstname-explanation" />
<p id="firstname-explanation">e.g. John</p>
但包括 label 中的所有内容对我来说似乎也不错(也提供了很好的样式能力,因为你有一个语义容器):
<label>
<span class="form-item-title">First Name</span>
<input type="text" id="firstname" />
<span class="form-item-description">e.g. John</span>
</label>
或者您甚至可以将两者混合。
另一种方法是将描述放在input 元素的title(或@Alohci 建议的placeholder)属性中(语义上这是描述它的那个),但在这种情况下,你有通过 Javascript(或使用input:after { content : "e.g. " attr(placeholder) } 的 CSS - @Alohci 建议)将其插入到标记中。
【讨论】:
<label for=""> 元素。
placeholder 仍然是一个有效的想法,但与这里的title 有同样的问题)。为了获得最佳效果,我仍然会在整个过程中使用一个 label,而不是您建议的 2 labels(这是完全有效的)。
input:after { content : "e.g. " attr(placeholder) }。这可能会通过额外的定位 css 来解决问题。
label 中:To associate a label with another control implicitly, the control element must be within the contents of the LABEL element. HTML5 spec 也是这么说的。