87,779
社区成员




var bodyElement = document.getElementsByTagName("body")[0];
for (var i=0; i<bodyElement.childNodes.length; i++) {
if (bodyElement.childNodes[i].nodeType == 8) {
alert(bodyElement.childNodes[i].nodeValue);
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<div>
<div>
<div>
<!-- note1 -->
</div>
</div>
<div>
<div></div>
</div>
</div>
<div>
<div>
<p>
<!-- note2 -->
<p>
</div>
<div>
<div></div>
</div>
</div>
<!-- note3 -->
<div>
<div>
<div></div>
</div>
<!-- note4 -->
<div>
<div></div>
</div>
</div>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<div>
<div>
<div>
<!-- note1 -->
</div>
</div>
<div>
<div></div>
</div>
</div>
<div>
<div>
<p>
<!-- note2 -->
<p>
</div>
<div>
<div></div>
</div>
</div>
<!-- note3 -->
<div>
<div>
<div></div>
</div>
<!-- note4 -->
<div>
<div></div>
</div>
</div>
<script type="text/javascript">
function eachComment(ele) {
for (var i=0; i<ele.childNodes.length; i++) {
var child = ele.childNodes[i];
if (child.nodeType == 8) {
alert(child.nodeValue);
} else if (child.childNodes) {
eachComment(child);
}
}
}
var bodyElement = document.getElementsByTagName("body")[0];
eachComment(bodyElement);
</script>
</body>
</html>