<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>test.html</title>
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<script type="text/javascript">
//作业 遍历整个文档的节点
window.onload = function() {
//获取根节点
var root = document.documentElement;
//调用递归的方法即可
items(root);
document.write(str);
}
var str = "";
function items(node) {
//得到当前节点的所有的孩子节点
if (node.nodeType == 1) { //判断是否是 元素节点
str += "节点名称:" + node.nodeName + "节点类型值:" + node.nodeType + "节点value值:"
+ node.nodeValue + "<br/>";
if (node.hasChildNodes()) { //此元素节点是否有孩子节点
var childs = node.childNodes;
for ( var i = 0; i < childs.length; i++) {
var child = childs.item(i);//得到孩子节点
items(child);
}
}
} else {
str += "节点名称:" + node.nodeName + "节点类型值:" + node.nodeType + "节点value值:"
+ node.nodeValue + "<br/>";
}
}
</script>
</head>
<body>
This is my HTML page.
<br>
</body>
</html>
javascript加强 遍历所有文档节点
最新推荐文章于 2022-06-23 19:04:15 发布