为什么标签不能包含标签?[英]Why tag can't contain tag inside it? 本文翻译自  Henry H Miao  查看原文  2011-12-06  58572    div/

时间:2022-06-26 21:03:43

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. And what about other tags' containment relationship?

第一个可以通过w3c validator (xhtml 1.0),但是第二个不能。我知道没有人会像第二个那样写代码。我只是想知道为什么。那么其他标签的包容关系呢?

5 个解决方案

#1


164  

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规范。它指定哪些元素是块元素,哪些元素是内联的。对于这些列表,搜索标有“HTML内容模型”的部分。

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

对于P元素,它指定了以下内容,这表明P元素只能包含内联元素。

<!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本身)”。

#2


52  

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中的

中放置

元素,因为打开
标签会自动关闭

元素。

#3


19  

According to HTML5, the content model of div elements is flow content

根据HTML5, div元素的内容模型是流内容。

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 used where flow content is expected.

其中包括p元素,这些元素只能在预期的流内容中使用。

Therefore, div elements can contain p elements.

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


However, the content model of p elements 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 content form paragraphs.

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

That doesn't include div elements, which can only be used where flow content is expected.

这并不包括div元素,它只能用于预期的流内容。

Therefore, p elements can't contain div elements.

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

Since the end tag of 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.

最后一个

是一个错误。

#4


0  

After the X HTML the conventions has be changed not its a mixture of conventions of XML and HTML so that is why the second approach is wrong and w3c validator accepts the things correct that are according to the standards and conventions .

在X HTML之后,这些约定不再是XML和HTML惯例的混合,这就是为什么第二种方法是错误的,而w3c验证器会根据标准和惯例接受正确的东西。

#5


-6  

because div tag has higher precedence then p tag. p tag represents for paragraph tag whether div tag represents document tag. you can write many paragraphs in a document tag but you can't write a document in a paragraph. Same a DOC file.

因为div标签的优先级高于p标签。p标签代表段落标签,是否div标签代表文档标签。您可以在一个文档标记中编写许多段落,但不能在段落中编写文档。同样一个DOC文件。

#1


164  

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规范。它指定哪些元素是块元素,哪些元素是内联的。对于这些列表,搜索标有“HTML内容模型”的部分。

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

对于P元素,它指定了以下内容,这表明P元素只能包含内联元素。

<!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本身)”。

#2


52  

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中的

中放置

元素,因为打开
标签会自动关闭

元素。

#3


19  

According to HTML5, the content model of div elements is flow content

根据HTML5, div元素的内容模型是流内容。

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 used where flow content is expected.

其中包括p元素,这些元素只能在预期的流内容中使用。

Therefore, div elements can contain p elements.

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


However, the content model of p elements 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 content form paragraphs.

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

That doesn't include div elements, which can only be used where flow content is expected.

这并不包括div元素,它只能用于预期的流内容。

Therefore, p elements can't contain div elements.

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

Since the end tag of 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.

最后一个

是一个错误。

#4


0  

After the X HTML the conventions has be changed not its a mixture of conventions of XML and HTML so that is why the second approach is wrong and w3c validator accepts the things correct that are according to the standards and conventions .

在X HTML之后,这些约定不再是XML和HTML惯例的混合,这就是为什么第二种方法是错误的,而w3c验证器会根据标准和惯例接受正确的东西。

#5


-6  

because div tag has higher precedence then p tag. p tag represents for paragraph tag whether div tag represents document tag. you can write many paragraphs in a document tag but you can't write a document in a paragraph. Same a DOC file.

因为div标签的优先级高于p标签。p标签代表段落标签,是否div标签代表文档标签。您可以在一个文档标记中编写许多段落,但不能在段落中编写文档。同样一个DOC文件。