嵌套 &lt;p&gt;嵌套 <div> 时不起作用将要? [英] Nesting &lt;p&gt; won&#39;t work while nesting &lt;div&gt; will?

查看:16
本文介绍了嵌套 &lt;p&gt;嵌套 <div> 时不起作用将要?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通常不会像这样嵌套<p>:

以下内容:<p>一个</p><p>二</p></p>

我会使用

来嵌套.但是今天我使用了 <p> 但似乎 Emacs 和 Google Chrome 都会认为外部 <p> 一旦看到新的 >

开始.(DOCTYPE 是 HTML 4.01 Strict).

我认为 <p> 只不过是一个

但只是有一些预定义的边距和填充,但是

不能嵌套?如果是这样,什么规则说它不能?

解决方案

因为一个段落就是一个段落 .. 这就是 HTML 的定义方式(而 HTML 不是 XML).

任何

(或其他块级元素)将隐式关闭任何打开的

.

根据 9.3.1 段落:P 元素 HTML 4.01 规范:

<块引用>

P 元素代表一个段落.它不能包含块级元素(包括 P 本身).

<小时>

请注意,这是解析 HTML 的方式,即使是

也会隐式关闭段落!

但是,带有 display:block; 不会关闭

作为 不是块级元素.

也就是说,在 HTML 处理的这个阶段,CSS 无关,并且在确定元素是否为块时,CSS 与 DOM/解析器无关 -级别元素与否.考虑动态应用 CSS 或通过尚未加载的样式表应用 CSS 的情况:应用的 CSS 不会改变 DOM.

<小时>

虽然 HTML5(工作草案)规范没有在 HTML4 规范中包含上述语言,但它确实继续将段落定义为 分阶段内容 并且还有一个关于段落的部分.

HTML5 列表的公认答案元素可以嵌套在 P 元素中吗? 表示

元素不能嵌套在 HTML5 中.文档中的关键短语是:短语内容的运行 [不包括

元素] 形成段落".此外,HTML5 试图在许多方面向后兼容,在 "对内容模型和属性值的限制":

<块引用>

某些元素的解析方式有些古怪(通常是出于历史原因),并且它们的内容模型限制旨在避免将作者暴露在这些问题中.

此行为引用自 关于流内容的 HTML5 WG 维基条目:

<块引用>

HTML5 对 p 元素的嵌套以及 p 元素可能包含的内容的限制是由于引用:解析器的特殊性"导致 p 自动关闭 ..

I usually won't nest <p> like this:

<p>The following:
    <p>one</p>
    <p>two</p>
</p>

and I will nest like that using <div> instead. But today I used <p> but it seems that both Emacs and Google Chrome both would consider the outer <p> closed as soon as it see a new <p> is started. (the DOCTYPE is HTML 4.01 Strict).

I thought <p> is no more than a <div> but just with some pre-defined margin and padding, but is it true that <p> cannot be nested? If so, what rule says that it cannot?

解决方案

Because a paragraph is a paragraph .. and that's how HTML is defined (and HTML is not XML).

Any <p> (or other block-level element) will implicitly close any open <p>.

Per 9.3.1 Paragraphs: the P element of the HTML 4.01 specification:

The P element represents a paragraph. It cannot contain block-level elements (including P itself).


Note that this is how the HTML is parsed and that even a <div> would have implicitly closed the paragraph!

However, a <span> with display:block; would not have closed the <p> as a <span> is not a block-level element.

That is, the CSS is irrelevant at this stage of the HTML processing and the CSS is irrelevant to the DOM/parser when determining if an element is a block-level element or not. Consider the case when CSS is applied dynamically or through a not-yet-loaded-stylesheet: the applied CSS does not alter the DOM.


While the HTML5 (working-draft) specification does not include the language above in the HTML4 specification, it does go on to define a paragraph as a container for phasing content and further has a section on paragraphs.

The accepted answer to List of HTML5 elements that can be nested inside P element? says that <p> elements cannot nest in HTML5. The key phrase from the documentation is: "Runs of phrasing content [which does not include <p> elements] form paragraphs". Furthermore, HTML5, trying to be backwards-compatible in many aspects, has a rationale on "Restrictions on content models and on attribute values":

Certain elements are parsed in somewhat eccentric ways (typically for historical reasons), and their content model restrictions are intended to avoid exposing the author to these issues.

This behavior is referenced from a HTML5 WG wiki entry on flow content:

HTML5's restrictions on nesting of p elements and on what p elements may contain, are due to, quote: "peculiarities of the parser" that causes p to be auto-closed ..

这篇关于嵌套 &lt;p&gt;嵌套 <div> 时不起作用将要?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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