访问< html>外部的评论节点标签 [英] Access comment nodes outside <html> tag
问题描述
我正在开发一个chrome扩展程序,该扩展程序可以从页面读取html注释并将其呈现在操作弹出窗口中.
I'm developing a chrome extension that reads html comments from a page and render them in an action popup.
但是,我不知道如何(以及是否有可能)获取<html>
标记之前和之后的一些注释.例如:
However, I don't know how to (and if it's possible) to fetch some comments that are before and after the <html>
tag. For example:
<!-- test test test -->
<DOCTYPE html>
<html>
...
</html>
在jQuery中,$("html").before()
和$("html").after()
都返回与$("html")
相同的结果.
In jQuery, $("html").before()
and $("html").after()
both returns the same as $("html")
.
是否可以使用jQuery或纯Javascript获取这些类型的注释?
Is it possible do fetch those types of comments using either jQuery or pure Javascript?
带有注释的页面如下:
<!-- Comment I'd like to fetch -->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-us" lang="en-us" >
<head>
<title>Featured Designers for WOMEN</title>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<meta http-equiv="Content-Script-Type" content="text/javascript" />
<meta http-equiv="Content-Style-Type" content="text/css" />
推荐答案
使用此文档:
<!-- comment 1 -->
<!DOCTYPE html>
<!-- comment 2 -->
<html>
<body>
hello world
</body>
</html>
您会收到第一个这样的评论:
You get the first comment like this:
document.firstChild.nodeValue
第二条评论如下:
document.childNodes[2].nodeValue
要获取document
下的所有注释:
var nodes = document.childNodes;
for (var i = 0, len = nodes.length; i !== len; ++i) {
if (nodes[i].nodeType === 8) {
console.log('Found comment' + nodes[i].nodeValue);
}
}
这篇关于访问< html>外部的评论节点标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!