为什么不允许在表单标签中直接使用输入标签?
我刚刚在 http://w3fools.com/#html_forms 阅读了以下内容:
在 HTML5 之前,非块级元素(例如
)直接在
我从来没有听说过任何类似的事情,而且我见过的每个基本 HTML 教程似乎都可以将输入标签直接放入表单标签中。所以我的问题分为三个部分:
- 上述说法合法吗?
- 为什么会这样呢? (这只是一个疏忽,还是 HTML 规范的创建者试图通过创建此规则来防止特定问题?)
- 构建带有输入的表单的推荐方法是什么? (我们是否应该直接在表单标签内创建一个 div 或一个表格?)
I just read the following at http://w3fools.com/#html_forms:
Non-block-level elements (such as
<input>
) are not valid directly inside<form>
tags until HTML5.
I had never heard of anything along these lines, and every basic HTML tutorial I've seen seems to be just fine with putting input tags directly inside a form tag. So my question has three parts:
- Is the above statement legitimate?
- Why is this the case? (Was it simply an oversight, or were the creators of the HTML spec trying to prevent specific problems by creating this rule?)
- What is the recommended way to construct a form with inputs? (Are we just supposed to create a div or a table directly inside the form tag?)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是标准的迂腐。
、
、
或其他一些块演示
内的元素。
It's standards pedantics.
<form>
specifies that it may only contain block elements or<script>
. As far as what every browser in the world allows, it's fine.<form>
to not be a layout tag at all, and they want all inline elements to be contained inside a block element.<div>
,<table>
,<p>
, or some other block presentational element inside the<form>
.上述说法属实。在 HTML 中,标签不是
The above statement is true. In HTML the <input> tag is not a valid element of the <form> tag. In order to make this validate, you need to enclose the <input> tag with either a <fieldset> or <div>. Which is demonstrated below.
首先我想提一下,HTML 教程教你做错事并不奇怪——HTML 实际上被设计为接受任何做事的方式。您可以不关闭标签,可以不正确地嵌套它们等等,这是我个人使用 XHTML 的原因之一。
这个说法似乎是正确的,但由于 HTML 的设计方式,它在实践中并不重要。 XHTML 可能禁止这样做。
表单实际上并不是任何类型的容器。 HTML 规范的创建者似乎很喜欢块级元素之类的东西,您应该将所有内容都包装在其中。不过,这只是我的看法,但据我所知,如果没有适当的容器,则不应使用非块级元素。
这就像您不应该将非块级元素放入
一个 div,一个表格——我认为即使是
也能完成这里的任务。
First of all I'd like to mention that it's not really surprising that HTML tutorials teach you to do things wrong – HTML was practically designed to accept any and every way of doing things. You can leave tags unclosed, you can nest them improperly and whatnot, which is one of the reasons I personally use XHTML.
That statement seems to be true, but because of how HTML is designed, it does not matter in practise. XHTML probably prohibits this.
Form isn't really a container of any sort. It seems like the creators of the HTML spec were fond of things like block-level elements you should wrap everything inside of. This is just my view on it, though, but as far as I've noticed, non-block-level elements shouldn't be used without a proper container for them.
It's exactly like you shouldn't put non-block-level elements in a
<blockquote>
. Block-level-elements are containers for other elements.A div, a table – I think even a
<p>
does the thing here.嗯,根据 HTML 4.01 规范(特别是 第 17.3 节),这在技术上是正确的。然而,我不知道有哪个网络浏览器会真正给你带来问题。
Well, according to the HTML 4.01 specification (specifically section 17.3), this is technically true. However, I don't know of any web browser that would actually give you a problem over it.