用于属性澄清的HTML5标签 [英] HTML5 label for attribute clarification
问题描述
来自HTML5最终规范的引用( http:// www .w3.org / TR / html5 / forms.html#attr-label-for ):
A quote from HTML5 final specification (http://www.w3.org/TR/html5/forms.html#attr-label-for):
<$ c可以指定$ c> for 属性以指示与标题相关联的表单控件。如果指定了该属性,则该属性的值必须是与该标签位于同一Document中的
labelable元素
的ID
元件。如果指定了该属性,并且Document中的元素ID
等于属性的值,和第一个这样的元素是
可标记元素
,然后该元素是标签元素的标记控件。
The
for
attribute may be specified to indicate a form control with which the caption is to be associated. If the attribute is specified, the attribute's value must be theID
of alabelable element
in the same Document as the label element. If the attribute is specified and there is an element in the Document whoseID
is equal to the value of thefor
attribute, and the first such element is alabelable element
, then that element is the label element's labeled control.
最后一句话措辞奇怪:它在单个元素中谈到第一个这样的元素并且似乎意味着可以有更多比具有相同ID的文档中的单个元素。粗体部分是否被视为和那个元素?
The last phrase is worded strangely: it talks about "the first such element" among a single element and seems to imply that there can be more than a single element in a document with the same ID. Was the part in bold meant to be read as "and that element"?
编辑。回答我的问题的人似乎相信规范正在定义一个行为,以防其中一个规范规则(唯一ID)被破坏。这对我来说听起来完全不可信。如果你违反了规范中的规则,那么你就要自担风险并获得未定义的行为。我从来没有看到过这样的规范:这是规则,但是如果你打破它,不要担心,事情仍然可以这样工作。
EDIT. People answering my question seems to believe the spec is defining a behavior in case one of the spec rules (unique IDs) is broken. This sounds totally implausible to me. If you break the rules in the spec you do it at your own peril and get the undefined behavior, period. Never have I seen the spec that says: here's the rule but if you break it, don't worry, things still gonna work like so and so.
推荐答案
似乎HTML5规范中引用的短语没有拼写错误,并且确实在其他规范规则之一(ID必须唯一)被破坏的情况下定义了一种行为。以下是另一个W3C规范(W3C DOM4)的引用,其中再次考虑了文档中存在重复ID的元素(注意粗体部分):
It seems the quoted phrase from HTML5 specification does not have a typo and indeed defines a behavior in case one of the other specification rules (IDs must be unique) is broken. Here is a quote from another W3C specification (W3C DOM4) worded in a way that again takes into account the existence of elements with a duplicate IDs in a document (notice the part in bold):
http://www.w3.org/TR/dom /#dom-nonelementparentnode-getelementbyid
getElementById(elementId)
方法必须以树的顺序在上下文对象的后代中返回第一个元素,其ID为elementId
,如果没有这样的元素,则返回null否则。
The
getElementById(elementId)
method must return the first element, in tree order, within context object's descendants, whose ID iselementId
, and null if there is no such element otherwise.
在规范规则之一(ID必须唯一)的情况下,W3C认为有必要定义行为的原因是然而,破碎仍然是一个谜。
The reason why the W3C deemed necessary to define a behavior in case one of the specification rules (IDs must be unique) is broken remains a mystery, though.
这篇关于用于属性澄清的HTML5标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!