为什么 FF DOM 可以/确实将节点视为 BODY 的子节点,即使它们位于 BODY 标记之前?
我观察到 Firefox 如何移动 HTML 文档中 BODY 元素之前列出的文本和元素节点,使其成为 BODY 元素的直接子元素。
我正在使用 xulrunner 2.0.1 (Firefox 4.0),虽然我观察到 IE 也移动文本,但不移动元素。
以下是 FF 执行此操作的一些示例:
示例 HTML 文档 1(文本节点和标题元素移动到正文内):
"<html>abc<title>def</title>hij<body>inn<span>e</span>r</body>klm</html>"
使用以下命令在 Body 元素上查询 nsIDOMNSHTMLElement.innerHTML 给出:
"abc<title>def</title>hijinn<span>e</span>rklm"
迭代 Body 子元素给出:
Text : "abc"
Element : "def"
Text : "hijinn"
Element : "e"
Text : "rklm"
< strong>示例 HTML 文档 2(文本节点移动到正文内,但标题未移动):
"<html><title>def</title>hij<body>inn<span>e</span>r</body>klm</html>"
使用以下命令在 Body 元素上查询 nsIDOMNSHTMLElement.innerHTML:
"hijinn<span>e</span>rklm"
迭代 Body 子 元素elements 给出:
Text : "hijinn"
Elemnt : "e"
Text : "rklm"
我的问题是为什么会发生这种情况?我本来期望innerHTML只是显示两个body标签之间的内容?
I have observed how firefox can move text and element nodes, listed before the BODY element in a HTML document, to become direct children of the BODY element.
I'm using xulrunner 2.0.1 (Firefox 4.0), Although I have observed IE also moving text, but not elements.
Here are some examples of FF doing this:
Example HTML Document 1 (text nodes and title element moved inside body):
"<html>abc<title>def</title>hij<body>inn<span>e</span>r</body>klm</html>"
Querying nsIDOMNSHTMLElement.innerHTML on the Body element using gives:
"abc<title>def</title>hijinn<span>e</span>rklm"
Iterating through Body child elements gives:
Text : "abc"
Element : "def"
Text : "hijinn"
Element : "e"
Text : "rklm"
Example HTML Document 2 (text node moved inside body but title isn't):
"<html><title>def</title>hij<body>inn<span>e</span>r</body>klm</html>"
Querying nsIDOMNSHTMLElement.innerHTML on the Body element using gives:
"hijinn<span>e</span>rklm"
Iterating through Body child elements gives:
Text : "hijinn"
Elemnt : "e"
Text : "rklm"
My question is why is this happening? I would have expected innerHTML just to display what between the two body tags?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是因为 BODY 标记是可选的,并且解析器假定它被省略。如果省略标签本身,那么 BODY 元素仍然会显示在 DOM 中,因为它的存在是隐含的。
HTML 4.01 规范指出BODY 元素的开始和结束标记都是可选的。
This is because the BODY tag is optional, and the parser assumes that it was omitted. If the tag itself is omitted, then the BODY element still shows up in the DOM, because its existance is implied.
The HTML 4.01 specification states that both the start and end tags are optional for the BODY element.
基本答案是“因为 HTML5 解析算法是这么说的”。更具体地说,只允许在
之外使用某些标记,而其他所有内容都会放入
中,即使它不存在于
中。原始数据流。
The basic answer is "because the HTML5 parsing algorithm says so". To be more specific, only certain tags are allowed outside of
<body>
, and everything else gets put inside<body>
even though it wasn't there in the original data stream.因为你给了它无效的 HTML 代码。 HTML 解析器将始终纠正无效的 HTML 代码。这尤其意味着</code> 标签将被移动到它们所属的 <code><head></code> 中,文本节点将被移动到 <code><body></code> 中。如果您对浏览器如何处理这种“标签汤”感兴趣,可以在 <a href="http://dev.w3.org/html5/spec/Overview.html#parsing" rel="nofollow" 下找到详细规范>http://dev.w3.org/html5/spec/Overview.html#parsing</a>。
标记唯一允许的子级是
和
。
Because you gave it invalid HTML code. HTML parsers will always correct invalid HTML code. This means in particular that the only allowed children of the
<html>
tag are<head>
and<body>
.<title>
tags will be moved into<head>
where they belong, text nodes into<body>
. If you are interested in how browsers handle this "tag soup", there is a detailed specification under http://dev.w3.org/html5/spec/Overview.html#parsing.