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

标签 javascript jquery html outerhtml

我这里有一个 jsfiddle - http://jsfiddle.net/stevea/QpNbu/3/ - 为所有具有 class='active' 的元素收集 outerHTML。令我吃惊的是,即使我注释掉了一些 HTML,如:

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

outerHTML 仍然把它带回来!这不可能仍然在 DOM 中,所以我想知道 outerHTML 在哪里。

感谢您的任何见解。

最佳答案

HTML 中的注释令人惊讶,事实上,它是 DOM 的一部分!如果您在此处查看此屏幕截图:

web inspector

您会看到 google chrome web inspector 将其呈现为 DOM 的一部分(请注意底部栏如何将其显示为 div#box1.active 的子项。)

有关更具体的文档,如果您查看 DOM 节点可能的 Node.nodeType on MDN ,您会看到 COMMENT_NODE 是有效类型之一。

COMMENT_NODE 8

W3还为 DOM 定义了 Comment 接口(interface):

This interface inherits from CharacterData and represents the content of a comment, i.e., all the characters between the starting '<!--' and ending '-->'. 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.

关于javascript - 为什么 outerHTML 会带回 &lt;!- - --> 评论?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17626273/

相关文章:

javascript - 使用 Flex 解析器解析 Javascript 代码

javascript - Angular JQuery UI 元素的可拖动副本

javascript - 点击div向下移动

javascript - 使用 javascript 检查 URL 是否存在

javascript - 如何让用户可以从 cli 安装我的测试包?

jquery - 揭穿.delay()

javascript - 使用 jquery 验证函数不适用于选择元素

html - 单页应用的滚动和非滚动版本

html - 如何将 UIImage 插入到包含 html 内容的电子邮件中?

javascript - 检查两个数组之间的元素是否相同,然后从中赋值