【问题标题】:Cannot read properties of undefined (reading 'tagName')无法读取未定义的属性(读取“tagName”)
【发布时间】:2022-01-15 13:04:10
【问题描述】:

我不知道为什么它返回 tagName 错误。子标签错了吗?

for (var ind = 0; ind < target.length; ind++)
    {
        var target = target[ind]
        for (var ind; ind < target.childNodes.length; ind++);
        {
            const subLabel = target.childNodes[ind];
            if (subLabel.tagName == "SPAN" || subLabel.tagName == "LI") //error line
            {
                //do something with label
            }
            typeDisplay(subLabel);
        }  
    }

【问题讨论】:

    标签: javascript html


    【解决方案1】:

    您使用了两次var ind。请改用let。阅读this

    【讨论】:

      【解决方案2】:

      嵌套循环通常使用不同的循环变量,例如外循环使用var i=0;,内循环使用var j=0;

      发布的代码,第二个var ind是重复的,因为它与第一个var共享相同的变量范围。

      【讨论】:

        【解决方案3】:

        您在内部和外部 for 循环中使用相同的变量名(即var ind)。

        你可以在这里做的是要么改变内部循环中的变量名,要么改变外部循环中的变量名。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2023-03-11
          • 2022-01-15
          • 2021-12-31
          • 2021-12-13
          • 2021-11-28
          • 2021-11-15
          • 2021-11-06
          相关资源
          最近更新 更多