javascript - 递归遍历 HTML DOM 并打印属性

标签 javascript html dom recursion

我正在尝试递归遍历 HTML 文档的 DOM 并打印节点的名称,同时使用缩进来标识子节点。我修改了一个 w3schools 代码示例,结果如下:

<!DOCTYPE html>
<html>
<head>
<title> Test Example </title>
</head>
<body>

<p>Click the button to get the node names of the body element's child nodes.</p>

<button onclick="myFunction(document.documentElement)">Try it</button>

<p><strong>Note:</strong> Whitespace inside elements is considered as #text, and text is considered as nodes.</p>

<!-- My personal comment goes here..  -->

<div><strong>Note:</strong> Comments in the document are considered as #comments, and comments are also considered as nodes.</div>

<p id="demo"></p>

<script>
function myFunction(node) {
indent = "";
txt = node.nodeName + "<br>";
document.write(txt);
var c = node.childNodes;
var i;
for (i = 0; i < c.length; i++) {
    myFunction(c[i], indent);
}    
}

function myFunction(node, indent) {
var indent = indent + "   ";
txt = indent + node.nodeName + "<br>";
document.write(txt);
var c = node.childNodes;
var i;
for (i = 0; i < c.length; i++) {
    txt = c[i].nodeName + "<br>"; 
    myFunction(c[i], indent);
}  
}
</script>

</body>
</html>

我得到的结果是:

undefined HTML
undefined HEAD
undefined #text
undefined TITLE
undefined #text
undefined #text
undefined #text
undefined BODY
undefined #text
undefined P
undefined #text
undefined #text
undefined BUTTON
undefined #text
undefined #text
undefined P
undefined STRONG
undefined #text
undefined #text
undefined #text
undefined #comment
undefined #text
undefined DIV
undefined STRONG
undefined #text
undefined #text
undefined #text
undefined P
undefined #text
undefined SCRIPT
undefined #text
undefined #text

所以我有几个问题 1) 为什么不打印带有节点名称的缩进值,而是打印 undefined? 2) 如果我只询问节点名称,为什么还要打印#text 和#comments 行?

我对 HTML 和 javascript 很陌生,所以任何见解都会有所帮助

编辑

我解决了第二个问题,但我的第一个问题仍然存在。我的脚本现在看起来像

<script>
function myFunction(node) {
    var indent = "";
    txt = node.nodeName + "<br>";
    document.write(txt);
    var c = node.children;
    var i;
    for (i = 0; i < c.length; i++) {
        myFunction(c[i], indent);
    }    
}

function myFunction(node, indent) {
    indent = indent + "    ";
    txt = indent + node.nodeName + "<br>";
    txt = txt.replace(/ /g, '&nbsp;');
    document.write(txt);
    var c = node.children;
    var i;
    for (i = 0; i < c.length; i++) {
        txt = c[i].nodeName + "<br>"; 
        myFunction(c[i], indent);
    }  
}

and my output looks like 

undefined    HTML
undefined        HEAD
undefined            TITLE
undefined        BODY
undefined            P
undefined            BUTTON
undefined            P
undefined                STRONG
undefined            DIV
undefined                STRONG
undefined            P
undefined            SCRIPT

我显然还遗漏了什么,但我不明白是什么

编辑2

感谢下面的帮助,我弄清楚了我在第二个 myfunction(现在称为 myfunction1)循环中调用错误函数的问题

<!DOCTYPE html>
<html>
<head>
<title> Test Example </title>
</head>
<body>

<p>Click the button to get the node names of the body element's child nodes.</p>

<button onclick="myFunction(document.documentElement)">Try it</button>

<p><strong>Note:</strong> Whitespace inside elements is considered as #text, and text is considered as nodes.</p>

<!-- My personal comment goes here..  -->

<div><strong>Note:</strong> Comments in the document are considered as #comments, and comments are also considered as nodes.</div>

<p id="demo"></p>

<script>
function myFunction(node) {
    var indent = "";
    var txt = node.nodeName + "<br>";
    document.write(txt);
    var c = node.children;
    var i;
    for (i = 0; i < c.length; i++) {
        myFunction1(c[i], indent);
    }    
}

function myFunction1(node, indent) {
    indent = indent + "      ";
    txt = indent + node.nodeName + "<br>";
    txt = txt.replace(/ /g, '&nbsp;');
    document.write(txt);
    var c = node.children;
    var i;
    for (i = 0; i < c.length; i++) {
        txt = c[i].nodeName + "<br>"; 
        myFunction1(c[i], indent);
    }  
}
</script>

</body>
</html>

现在输出

HTML
      HEAD
            TITLE
      BODY
            P
            BUTTON
            P
                  STRONG
            DIV
                  STRONG
            P
            SCRIPT

最佳答案

1) why is it not printing the indent value with the node name and instead printing undefined?

您有 2 个同名函数,请确保您运行的是正确的函数并提供正确的参数。从更新的输出中,您可以使用这样的代码在第一次调用时将 indent 初始化为空字符串。

function myFunction(node, indent) {
    indent = (indent || "") + "    ";

2) why is it printing the #text and #comments lines as well if I'm only asking for the nodes name?

因为这些也是节点(并且它们有名称),并且您没有在代码中过滤掉它们。考虑仅通过元素递归,而不是所有 DOM 节点,如果您想排除它们,则使用 .children 而不是 .childNodes

关于javascript - 递归遍历 HTML DOM 并打印属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36098066/

相关文章:

javascript - 文本选择跳转到顶部

javascript - 文本附加和循环效果

javascript - 如何在 Javascript 中跳过循环中的单词

javascript - 从外部文件加载引导表

javascript - 如何在javascript中每10秒调用一次函数?

javascript - 我怎样才能让我的 JavaScript(如果它是正确的)在倒计时后执行?

html - 无法在 ionic 中以 ionic 滚动方向 ='x' 垂直滚动

javascript - 如何将 PhantomJS 作为服务器运行并远程调用它?

javascript - 等待 http.get 返回对象

javascript - 轻松清除所有脚本设置变量 (Javascript/jQuery)