HTML 属性:for
for
属性是 <label>
和 <output>
允许的属性。当用于 <label>
元素时,它指示该标签描述的表单元素。当用于 <output>
元素时,它允许表示输出中使用的值的元素之间存在显式关系。
¥The for
attribute is an allowed attribute for <label>
and <output>
. When used on a <label>
element it indicates the form element that this label describes. When used on an <output>
element it allows for an explicit relationship between the elements that represent values which are used in the output.
Try it
用法
¥Usage
当用作 <label>
的属性时,for
属性的值是与其相关的表单元素的 id
。
¥When used as an attribute of <label>
, the for
attribute has a value which is the id
of the form element it relates to.
<label for="username">Your name</label> <input type="text" id="username" />
当用作 <output>
的属性时,for
属性的值是用于创建输出的元素的 id
值的空格分隔列表。
¥When used as an attribute of <output>
, the for
attribute has a value which is a space separated list of the id
values of the elements which are used to create the output.
<input type="range" id="b" name="b" value="50" /> +
<input type="number" id="a" name="a" value="10" /> =
<output name="result" for="a b">60</output>
示例
规范
Specification |
---|
HTML Standard # attr-label-for |
HTML Standard # attr-output-for |
浏览器兼容性
¥Browser compatibility
html.elements.label.for
BCD tables only load in the browser
html.elements.output.for
BCD tables only load in the browser