为什么 W3C 建议将输入元素包装在
中?标签?
我在网上看到了很多例子,其中表单的布局如下:
<form>
<p><input></p>
</form>
令我惊讶的是,这也在 规范:
任何表单都以表单元素开头,其中放置 控制。大多数控件由输入元素表示,该元素由 默认提供单行文本字段。要为控件添加标签,请使用标签 使用元素;标签文本和控件本身位于 标签元素。表格的每个部分都被视为一个段落,并且是 通常使用 p 元素与其他部分分开。把这个 综上所述,以下是询问客户姓名的方式:
尽管本节是非规范性的,但在我看来,这是一种不好的做法,而不是语义。我想目的是将输入放在自己的行上,但是这些元素的显示不应该使用 CSS 来控制吗?
W3C 建议以这种方式布局表单是否有原因?我错过了什么吗?
I've seen a lot of examples on the web where forms are laid out like so:
<form>
<p><input></p>
</form>
To my surprise, this is also described in the specification:
Any form starts with a form element, inside which are placed the
controls. Most controls are represented by the input element, which by
default provides a one-line text field. To label a control, the label
element is used; the label text and the control itself go inside the
label element. Each part of a form is considered a paragraph, and is
typically separated from other parts using p elements. Putting this
together, here is how one might ask for the customer's name:
Though this section is non-normative, it still seems to me that this is bad practice and not semantic. I suppose that the purpose is to put inputs on their own line, but shouldn't the display of these elements be controlled using CSS?
Is there a reason why the W3C advises forms be laid out this way? Am I missing something?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您以有意义(读:语义)的方式编写表单,您将希望文本流通向该元素:
更好的方法是将您的表单视为 mad-libs 脚本:
p
元素并不是标记表单的唯一方法。如果要添加/编辑数据矩阵,则使用表在语义上是有效的。在其他情况下,您可能不想使用包装元素。这取决于您想要提供的内容。整理好内容后,再担心如何设计所有内容。
If you are writing a form in a meaningful (read: semantic) way, you will want the flow of text to lead to the element:
An even better way is to treat your form like a mad-libs script:
A
p
element isn't the only way to mark-up a form. If a matrix of data is being added/edited, it's semantically valid to use a table.In other cases, you might not want to use a wrapping element. It depends on what content you want to be serving up. Worry about styling it all after you've got your content sorted out.
INPUT 元素是内联的,因此将它们包装在某种块元素中是有意义的,以便它们之间有自然的分离。由于 DIV 默认情况下没有边距,因此使用段落是有意义的。
INPUT elements are inline, and therefore it makes sense to wrap them in some sort of block element, so that there is a natural separation between them. Since the DIV has no margins by default, it makes sense to use a paragraph.
领先的 CSS、HTML 和网格框架均未使用
P
标签。请参阅此处:Bootstrap 或 基金会。
恕我直言,使用 fieldset 和 div。
虽然 W3C 建议使用段落标签
,但遵循他们的建议几乎没有任何意义。原因是:你限制了自己。也就是说:如果您使用
p
标签编写组件,您将无法在其中放置 div。 将;添加额外的
那么,为什么要将 div 放在
p
标记内呢?好吧...为什么不呢?例如,您想以某种方式设置内容的样式,或添加一些信息。使用p
标签,您现在陷入困境。对我来说这似乎也是个糟糕的建议。答案:
它有点假设你知道从现在起一年后你会需要什么。
我的建议:你不想这样:
“我希望我没有使用限制性
P
标签”None of the leading CSS, HTML, and grid frameworks is using
P
tags.See here: Bootstrap or Foundation.
IMHO, use fieldset and divs.
While W3C recommend using paragraph tags
<p>
, it makes little to no sense to follow their advice. The reason is: you are restricting yourself. That is: if you write your components usingp
tags you won't be able to put a div inside it. Putting <div> inside <p> is adding an extra <p>well, why would you want to put a div inside a
p
tag? well... why not? for example you want to style your content in a way, or add some information. Usingp
tags you are stuck now. It seems to be bad advice as well to me.The answer:
It kind of assumes that you know what you will need in, say, one year from now.
My advice: you don't want to be like:
"I wish I hadn't used the restrictive
P
tags"这适用于 HTML 4,但可能不适用于所请求的 HTML 5。
参考:17.3 FORM 元素
form
需要一个块级元素作为子元素。input
是一个内联元素。p
就可以解决这个问题。This goes for HTML 4, but maybe not for the requested HTML 5.
Ref.: 17.3 The FORM element
form
needs a block-level element as child.input
is an inline element. Thep
does the trick.