为什么不允许在表单标签中直接使用输入标签?

发布于 2024-10-27 04:03:24 字数 445 浏览 2 评论 0原文

我刚刚在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

骄傲 2024-11-03 04:03:24

这是标准的迂腐。

  • 就标准而言,该声明是合法的:在 HTML 4.01 中,
    的定义指定它只能包含块元素或
  • 我只能猜测他们认为
    根本不是布局标签,并且他们希望所有内联元素都包含在块元素内。
  • 是的,您应该放置

    或其他一些块演示

    内的元素。

It's standards pedantics.

  • The statement is legitimate as far as the standard goes: in HTML 4.01, the definition for <form> specifies that it may only contain block elements or <script>. As far as what every browser in the world allows, it's fine.
  • I can only guess that they consider <form> to not be a layout tag at all, and they want all inline elements to be contained inside a block element.
  • Yes, you're supposed to place a <div>, <table>, <p>, or some other block presentational element inside the <form>.
¢好甜 2024-11-03 04:03:24

上述说法属实。在 HTML 中,标签不是

的有效元素标签。为了使其验证,您需要将 括起来。带有

的标签或

。下面将对此进行演示。
<form action="/" method="post">
    <fieldset>
        Field: <input type="text" name="field" />
        <br />
        <input type="submit" value="Submit" />
    </fieldset>
</form>

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.

<form action="/" method="post">
    <fieldset>
        Field: <input type="text" name="field" />
        <br />
        <input type="submit" value="Submit" />
    </fieldset>
</form>
┈┾☆殇 2024-11-03 04:03:24

首先我想提一下,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.

三月梨花 2024-11-03 04:03:24

嗯,根据 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文