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
Why can't the <p> tag contain a <div> tag inside it?
提问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 div
elements is flow content
Most elements that are used in the body of documents and applications are categorized as flow content.
文档和应用程序正文中使用的大多数元素都归类为流内容。
That includes p
elements, which can only be usedwhere flow contentis expected.
Therefore, div
elements can contain p
elements.
因此,div
元素可以包含p
元素。
However, the content modelof p
elements is Phrasing content
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 div
elements, which can only be usedwhere flow contentis expected.
Therefore, p
elements can't contain div
elements.
因此,p
元素不能包含div
元素。
Since the end tagof p
elements can be omitted when the p
element is immediately followed by a div
element (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 div
tag has higher precedence than the p
tag. The p
tag represents a paragraph tag whereas the div
tag 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 文件相同。