嵌套< p>在嵌套< div>时不起作用将? [英] Nesting <p> won't work while nesting <div> will?

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

问题描述

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

 < p>以下内容:
< p> one< / p>
< p> two< / p>
< / p>

我将使用< div> 代替。但今天我使用了< p> ,但似乎Emacs和Google Chrome都会考虑外部< p> 只要它看到一个新的< p> ,就立即关闭。 ( DOCTYPE 是HTML 4.01 Strict)。



我以为< p> 不超过一个< div> ,但只是有一些预定义的边距和填充,但确实是 < p> 不能嵌套?如果是这样,什么规则说它不能?

解决方案

因为段落是一个段落,并且这就是HTML定义的方式而HTML则不是XML)。



任何< p> em> other 块级元素)会隐式关闭任何打开< p>



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


P元素表示一个段落。 不能包含块级元素(包括P本身)。






请注意,这是如何解析,即使是< div> 也会隐含地关闭段落!然而,< span> 显示:block; 已将< p> 关闭为< span> 不是块级元素。



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






尽管HTML5(working-draft)规范在HTML4规范中没有包含上述语言,但它确实将段落定义为容器用于阶段性内容,并且还有一个段落

href =https://stackoverflow.com/questions/9852312/list-of-html5-elements-that-c​​an-be-nested-inside-p-element>可以嵌套在P元素中的HTML5元素列表? 表示< p> 元素不能嵌套在HTML5中。文档中的关键词是:短语内容的运行[不包括< p> 元素]形成段落。此外,试图在许多方面向后兼容的HTML5有一个基于限制内容模型和属性值 b
$ b


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


此行为参考 HTML5工作组关于流程内容的wiki条目


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

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