简体   繁体   English

为什么outerHTML会带回<! - - - >评论?

[英]Why does outerHTML bring back <!-​- --> comments?

I have a jsfiddle here - http://jsfiddle.net/stevea/QpNbu/3/ - that collects the outerHTML for all elements that have class='active' . 我在这里有一个jsfiddle - http://jsfiddle.net/stevea/QpNbu/3/ - 它为所有具有class='active'元素收集outerHTML What amazes me is that even if I comment out some of the HTML, as in: 令我惊讶的是,即使我注释掉了一些HTML,例如:

<!--      <div>'Some inner text'</div>  -->

outerHTML still brings it back! outerHTML仍然带回来! This can't still be in the DOM so I'm wondering where outerHTML is looking. 这还不能在DOM中,所以我想知道outerHTML在哪里。

Thanks for any insight. 感谢您的任何见解。

Comments in HTML are surprisingly , in fact, part of the DOM! HTML中的注释令人惊讶 ,实际上是DOM的一部分! If you look in this screen shot here: 如果你看这个屏幕截图:

网络检查员

You'll see the google chrome web inspector renders it as part of the DOM (notice how the bottom bar shows it as a child of div#box1.active as well.) 您会看到谷歌浏览器网页检查器将其呈现为DOM的一部分(请注意底栏如何将其显示为div#box1.active的子项。)

For more concrete documentation, if you look at the possible Node.nodeType s for DOM nodes on MDN , you'll see that the COMMENT_NODE is one of the valid types. 有关更具体的文档,如果您查看MDN上 DOM节点的可能Node.nodeType ,您将看到COMMENT_NODE是有效类型之一。

COMMENT_NODE 8 COMMENT_NODE 8

W3 also defines the Comment interface for the DOM: W3还定义了DOM的Comment接口:

This interface inherits from CharacterData and represents the content of a comment, ie, all the characters between the starting '<!--' and ending '-->'. 此接口继承自CharacterData并表示注释的内容,即起始“<! - ”和结束“ - >”之间的所有字符。 Note that this is the definition of a comment in XML, and, in practice, HTML, although some HTML tools may implement the full SGML comment structure. 请注意,这是XML中注释的定义,实际上是HTML,尽管某些HTML工具可能会实现完整的SGML注释结构。

HTML注释是DOM的一部分,它们只是被浏览器忽略才能显示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM