为什么< p>标记不能包含< div>里面有标签? [英] Why <p> tag can't contain <div> tag inside it?

查看:142
本文介绍了为什么< p>标记不能包含< div>里面有标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我所知,
这是正确的:

As far as I know, this is right:

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

但这是错误的:

But this is wrong:

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

第一个可以通过w3c验证器(xhtml 1.0),但第二个不能。
我知道没有人会写第二个代码。我只想知道为什么。
其他标签的包含关系怎么样?

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?

推荐答案

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

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".

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

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

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

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

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)."

这篇关于为什么&lt; p&gt;标记不能包含&lt; div&gt;里面有标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆