为什么流浪< / p>结束标签生成一个空的段落? [英] Why does a stray </p> end tag generate an empty paragraph?

查看:149
本文介绍了为什么流浪< / p>结束标签生成一个空的段落?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

显然,如果您在正文中没有匹配的起始标记的< / p> 结束标记元素,大多数(如果不是全部)浏览器将在其中生成一个空的段落:

Apparently, if you have a </p> end tag with no matching start tag within the body element, most if not all browsers will generate an empty paragraph in its place:

<!DOCTYPE html>
<title></title>
<body>
</p>
</body>

即使结尾标签中存在任何文本,也不会将其作为 p 元素 - 它将始终为空,文本节点将始终存在:

Even if any text exists around the end tag, none of it is made part of this p element — it will always be empty and the text nodes will always exist on their own:

<!DOCTYPE html>
<title></title>
<body>
some text</p>more text
</body>

如果 body 的上述内容被包装在< p> < / p> 标签...我会让你猜到发生什么:

If the above contents of body are wrapped in <p> and </p> tags... I'll leave you to guess what happens:

<!DOCTYPE html>
<title></title>
<body>
<p>some text</p>more text</p>
</body>

>标签之前没有一个< body> < / body> 标签,所有浏览器除了IE9和较旧的不会生成一个空的段落(IE≤9,另一方面将始终创建一个,而IE10和以后的行为与所有其他浏览器的行为一样):

Interestingly, if the </p> tag is not preceded by a <body> or </body> tag, all browsers except IE9 and older will not generate an empty paragraph (IE ≤ 9 on the other hand will always create one, while IE10 and later behave the same as all other browsers):

<!DOCTYPE html>
<title></title>
</p>





<!DOCTYPE html>
<title></title>
</p><body>





<!DOCTYPE html>
<title></title>
</p></body>

我找不到任何引用,规定没有相应开始标签的结束标签应该生成一个空的元素,但是这不应该像一个令人惊讶的考虑,因为它甚至不是有效的HTML首先。实际上,我只发现浏览器使用 p 元素(在某种程度上 br 元素好的!),但是没有任何解释为什么。

I can't find any references stipulating that an end tag with no corresponding start tag should generate an empty element, but that shouldn't come across as surprising considering that it's not even valid HTML in the first place. Indeed, I've only found browsers to do this with the p element (and to some extent the br element as well!), but not any explanation as to why.

在使用传统HTML解析器和HTML5解析器的浏览器中,这一点是相当一致的,尽管它们在怪异的模式和在标准模式下。所以,推断这是为了向后兼容早期规范或传统行为可能是公平的。

It is rather consistent across browsers using both traditional HTML parsers and HTML5 parsers, though, applying both in quirks mode and in standards mode. So, it's probably fair to deduce that this is for backward compatibility with early specifications or legacy behavior.

其实我确实发现了这个评论,基本上证实了这一点:

In fact, I did find this comment on an answer to a somewhat related question, which basically confirms it:


< p为H.标签有效未关闭是原始< p>被定义为新段落标记,而不是p是容器元素。相当于< br>作为新行标记。您可以从1992年起在本文档中看到如下定义: http ://www.w3.org/History/19921103-hypertext/hypertext/WWW/MarkUp/Tags.html ,从1993年开始: http://www.w3.org/MarkUp/draft-ietf-iiir-html-01.txt 因为有改变前的网页和浏览器解析器一直与现有的网页内容尽可能向后兼容,但始终可以使用< p>

The reason why <p> tags are valid unclosed is that originally <p> was defined as a "new paragraph" marker, rather than p being a container element. Equivalent to <br> being a "new line" marker. You can see so defined in this document from 1992:http://www.w3.org/History/19921103-hypertext/hypertext/WWW/MarkUp/Tags.html and this one from 1993: http://www.w3.org/MarkUp/draft-ietf-iiir-html-01.txt Because there were web pages pre-dating the change and browser parsers have always been as backward compatible as possible with existing web content, it's always stayed possible to use <p> that way.

但这并不完全解释为什么解析器对待一个显式的< / p> / code>结束标签(带斜杠)只是一个标签,并在DOM中生成一个空的元素。当语法没有被严格定义为更多 最近或者其他的东西?如果是,是否记录在任何地方?

But it doesn't quite explain why parsers treat an explicit </p> end tag (with the slash) as simply... a tag, and generate an empty element in the DOM. Is this part of some parser error handling convention from way back when the syntax wasn't as strictly defined as it was more recently or something? If so, is it documented anywhere at all?

推荐答案

。请参阅 http://dev.w3.org/html5/ spec / tree-construction.html#parsing-main-inbody 并搜索一个标签名称为p的结束标签,它说: p>

That it is required is documented in HTML5. See http://dev.w3.org/html5/spec/tree-construction.html#parsing-main-inbody and search down for An end tag whose tag name is "p" and it says:


如果打开元素的堆栈在按钮范围
中没有与令牌相同的标签名称的元素,那么这是一个解析
错误;如果已经看到标签名为p的起始标签,则
重新处理当前标记。

If the stack of open elements does not have an element in button scope with the same tag name as that of the token, then this is a parse error; act as if a start tag with the tag name "p" had been seen, then reprocess the current token.

如果< / p> 标记不能匹配,则翻译成英文意味着创建一个 p 元素现有的< p> 标签。

Which translated into English means create a p element if the </p> tag can't be matched with an existing <p> tag.

为什么是这样,更难探明。通常,这是因为以前的一些浏览器导致这种情况发生在一个错误中,网页依赖于这种行为,所以其他浏览器也必须实现它。

Why it is so, is harder to ascertain. Usually, this is because some browser in the past caused this to happen as a bug, and web pages came to rely on the behaviour, so other browsers had to implement it too.

这篇关于为什么流浪&lt; / p&gt;结束标签生成一个空的段落?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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