在 HTML 表单中,标签是针对输入的名称还是 ID?
In HTML forms does the label target the name or the id of the input?
这个简单的困惑让我困惑了很长时间,因为在我见过的所有示例中,label 标签中的 'for' 属性值与 'id' 标签和 'name' 输入的标签。
它针对 id。
https://www.w3schools.com/tags/tag_label.asp
Specifies the id of the form element the label should be bound to
标签中的属性for
,针对输入的id
。
一些定义:
<input>
name: Name of the input form control. Submitted with the form as part
of a name/value pair.
id: Global attribute valid for all elements, including all the input
types, it defines a unique identifier (ID) which must be unique in the
whole document. Its purpose is to identify the element when linking.
The value is used as the value of the <label>
's for attribute to
link the label with the form control.
<label>
for: The id of a labelable form-related element in the same document
as the <label>
element. The first element in the document with an id
matching the value of the for attribute is the labeled control for
this label element, if it is a labelable element. If it is not
labelable then the for attribute has no effect. If there are other
elements which also match the id value, later in the document, they
are not considered.
更多信息:input element and label element
这个简单的困惑让我困惑了很长时间,因为在我见过的所有示例中,label 标签中的 'for' 属性值与 'id' 标签和 'name' 输入的标签。
它针对 id。
https://www.w3schools.com/tags/tag_label.asp
Specifies the id of the form element the label should be bound to
标签中的属性for
,针对输入的id
。
一些定义:
<input>
name: Name of the input form control. Submitted with the form as part of a name/value pair.
id: Global attribute valid for all elements, including all the input types, it defines a unique identifier (ID) which must be unique in the whole document. Its purpose is to identify the element when linking. The value is used as the value of the
<label>
's for attribute to link the label with the form control.
<label>
for: The id of a labelable form-related element in the same document as the
<label>
element. The first element in the document with an id matching the value of the for attribute is the labeled control for this label element, if it is a labelable element. If it is not labelable then the for attribute has no effect. If there are other elements which also match the id value, later in the document, they are not considered.
更多信息:input element and label element