Html 为什么 <p> 标签中不能包含 <div> 标签?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8397852/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 11:50:30  来源:igfitidea点击:

Why can't the <p> tag contain a <div> tag inside it?

htmlxhtml

提问by Henry H Miao

As far as I know, this is right:

据我所知,这是正确的:

<div>
  <p>some words</p>
</div>

But this is wrong:

但这是错误的:

<p>
  <div>some words</div>
</p>

The first one can pass the W3C validator(XHTML 1.0), but the second can't. I know that nobody will write code like the second one. I just want know why.

第一个可以通过W3C 验证器(XHTML 1.0),但第二个不能。我知道没有人会像第二个那样编写代码。我只是想知道为什么。

And what about other tags' containment relationship?

那么其他标签的包含关系呢?

回答by Colin Campbell

An authoritative place to look for allowed containment relations is the HTML spec. See, for example, http://www.w3.org/TR/html4/sgml/dtd.html. It specifies which elements are block elements and which are inline. For those lists, search for the section marked "HTML content models".

寻找允许的包含关系的权威场所是 HTML 规范。例如,参见http://www.w3.org/TR/html4/sgml/dtd.html。它指定哪些元素是块元素,哪些是内联元素。对于这些列表,搜索标记为“HTML 内容模型”的部分。

For the P element, it specifies the following, which indicates that P elements are only allowed to contain inline elements.

对于 P 元素,它规定了以下内容,表示 P 元素只允许包含 inline 元素。

<!ELEMENT P - O (%inline;)*            -- paragraph -->

This is consistent with http://www.w3.org/TR/html401/struct/text.html#h-9.3.1, which says that the P element "cannot contain block-level elements (including P itself)."

这与http://www.w3.org/TR/html401/struct/text.html#h-9.3.1一致,即 P 元素“不能包含块级元素(包括 P 本身)”。

回答by defau1t

In short, it is impossible to place a <div>element inside a <p>in the DOM because the opening <div>tag will automatically close the <p>element.

简而言之,在 DOM 中不可能将<div>元素放在 a<p>内,因为开始<div>标签会自动关闭该<p>元素。

回答by Oriol

According to HTML5, the content modelof divelements is flow content

根据HTML5,所述内容模型div元素是流内容

Most elements that are used in the body of documents and applications are categorized as flow content.

文档和应用程序正文中使用的大多数元素都归类为流内容。

That includes pelements, which can only be usedwhere flow contentis expected.

这包括p元素,这些元素只能在需要流内容的地方使用

Therefore, divelements can contain pelements.

因此,div元素可以包含p元素。



However, the content modelof pelements is Phrasing content

然而,内容模型p元素是段落式内容

Phrasing content is the text of the document, as well as elements that mark up that text at the intra-paragraph level. Runs of phrasing contentform paragraphs.

短语内容是文档的文本,以及在段落内标记该文本的元素。运行的措辞内容形式的段落

That doesn't include divelements, which can only be usedwhere flow contentis expected.

这不包括div元素,这些元素只能在需要流内容的地方使用

Therefore, pelements can't contain divelements.

因此,p元素不能包含div元素。

Since the end tagof pelements can be omitted when the pelement is immediately followed by a divelement (among others), the following

由于结束标记p可以当被省略的元件p元件之后紧接着一个div元件(除了别的以外),以下

<p>
  <div>some words</div>
</p>

is parsed as

被解析为

<p></p>
<div>some words</div>
</p>

and the last </p>is an error.

最后一个</p>是错误。

回答by geekdev786

After the X HTML, the conventions has been changed, and now it's a mixture of conventions of XML and HTML, so that is why the second approach is wrong and the W3C validator accepts the things correct that are according to the standards and conventions.

在 X HTML 之后,约定发生了变化,现在是 XML 和 HTML 约定的混合,这就是为什么第二种方法是错误的,W3C 验证器接受符合标准和约定的正确事物。

回答by Gaurav Agrawal

Because the divtag has higher precedence than the ptag. The ptag represents a paragraph tag whereas the divtag represents a document tag.

因为div标签的优先级高于p标签。该p标签代表一个段落标签,而div标签代表一个文档标签。

You can write many paragraphs in a document tag, but you can't write a document in a paragraph. The same as a DOC file.

您可以在一个文档标签中编写多个段落,但不能在一个段落中编写一个文档。与 DOC 文件相同。