【发布时间】:2014-05-20 16:10:04
【问题描述】:
在 HTML 标准中,为什么 HTML label 元素已经定义了 form 属性,而它已经具有 for 属性?它有什么目的?
如果我有一个标签元素,其中子内容是输入类型或使用了for 属性,form 属性有什么用途?
例如,在每个label 场景中,通过查看子元素或父元素可以知道“拥有表单”。是否存在另一种情况,即带有form 属性的label 既有用又不会增加混乱?
<body>
<form action="#" id="form-one" method="post">
<label>Dog name<input type="text" id="tb-dog-name"></label>
<button type="submit" id="form-one-submit">Post Dog Name</button>
</form>
<form action="#" id="form-two" method="post">
<label for="tb-cat-name">Cat name</label>
<input type="text" id="text-box-two">
<button type="submit" id="form-two-submit">Post Cat Name</button>
</form>
<label for="tb-pizza">How much do you like pizza?</label>
<input type="text" id="tb-pizza" form="form-three">
<form action="#" id="form-three" method="post">
<button type="submit" id="form-three-submit">Share your love!</button>
</form>
</body>
【问题讨论】:
-
@j08691 w3.org/TR/html-markup/label.html#label.attrs.form 我不知道它是干什么用的……
-
@WesleyMurch - HTML5 中的新功能。
-
我的意思是,我用了 2 秒的时间谷歌了它:impressivewebs.com/html5-form-attribute
-
@WesleyMurch — 讨论表单控件的表单属性(如输入),而不是标签。
-
@Quentin 啊,是的,我明白了。老实说,这似乎真的很粗略,永远不会使用。当您不知道自己在做什么时,这似乎是一种惹麻烦的好方法。