简体   繁体   English

为什么W3C建议包装输入元素 <p> 标签?

[英]Why does the W3C advise wrapping input elements in <p> tags?

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 spec : 令我惊讶的是,这也在规范中描述:

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. 大多数控件由input元素表示,默认情况下提供单行文本字段。 To label a control, the label element is used; 要标记控件,使用标签元素; the label text and the control itself go inside the label element. 标签文本和控件本身进入label元素。 Each part of a form is considered a paragraph, and is typically separated from other parts using p elements. 表单的每个部分都被视为段落,并且通常使用p元素与其他部分分开。 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? 我想目的是将输入放在他们自己的行上,但不应该使用CSS控制这些元素的显示?

Is there a reason why the W3C advises forms be laid out this way? 有没有理由为什么W3C会以这种方式建议表格? Am I missing something? 我错过了什么吗?

If you are writing a form in a meaningful (read: semantic) way, you will want the flow of text to lead to the element: 如果您正在以有意义的(读取:语义)方式编写表单,则需要将文本流引导到该元素:

<form>
 <p><label for="firstName">Please enter your first name:</label><input id="firstName" type="text" /></p>
</form>

An even better way is to treat your form like a mad-libs script: 更好的方法是将表单视为mad-libs脚本:

<form>
  <p>Hello. My <label for="firstName">name</label> is <input id="firstName" type="text" />...</p>
</form>

A p element isn't the only way to mark-up a form. p元素不是标记表单的唯一方法。 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 elements are inline, therefore it makes sense to wrap them in some sort of block element, so that there is a natural separation between them. INPUT元素是内联的,因此将它们包装在某种块元素中是有意义的,因此它们之间存在自然的分离。 Since the DIV has no margins by default, doesn't it make sense to use a paragraph? 由于DIV默认没有边距,使用段落是否有意义?

None of the leading css/html/grid frameworks is using P tags 领先的css / html / grid框架都没有使用P标签

see here bootstrap or foundation 看到这里的bootstrap基础

imho use fieldset and divs. imho使用fieldset和div。

while w3c recommend using paragraph tags <p> it makes little to no sense to follow their advice. 虽然w3c建议使用段落标签<p>但是根据他们的建议几乎没有任何意义。 The reason is: you are restricting yourself. 原因是:你在限制自己。 That is: if you write your components using p tags you won't be able to put a div inside it. 也就是说:如果使用p标签编写组件,则无法在其中放置div。 Putting <div> inside <p> is adding an extra <p> 将<div>放在<p>中是添加一个额外的<p>

well, why would you want to put a div inside a p tag? 好吧,你为什么要把一个div放在一个p标签里? well... why not? 好吧......为什么不呢? for example you want to style your content in a way, or add some information. 例如,您希望以某种方式设置内容样式,或添加一些信息。 Using p tags you are stuck now. 使用p标签你现在卡住了。 It seems to be bad advice as well to me. 对我来说这似乎也是一个糟糕的建议。

The answer: 答案:

worry about styling once you got your content sorted out 一旦你整理了你的内容,你就会担心造型问题

kind of assumes that you know what you will need in, say, 1 year from now. 有点假设你知道你需要什么,比如说,1年后。

my advice: you don't want to be like 我的建议:你不想成为

"I wish I hadn't used the restrictive P tags" “我希望我没有使用限制性P标签”

This goes for HTML 4, maybe not for requested HTML 5. 这适用于HTML 4,可能不适用于请求的HTML 5。

http://www.w3.org/TR/html401/interact/forms.html#edef-FORM http://www.w3.org/TR/html401/interact/forms.html#edef-FORM

form needs a block-level element as child. form需要一个块级元素作为子元素。 input is an inline element. input是内联元素。 The p does the trick. p诀窍。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM