最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Cannot read properties of undefined (reading 'tagName') - Stack Overflow

matteradmin8PV0评论

I don't know why it's returning error with tagName. is subLabel wrong?

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);
        }  
    }

I don't know why it's returning error with tagName. is subLabel wrong?

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);
        }  
    }
Share Improve this question asked Jan 15, 2022 at 13:04 eattatoeattato 131 gold badge1 silver badge5 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 1

You are using var ind twice. Use let instead. Read this

You are using the same variable name in both inner and outer for loop (ie. var ind).

what you can do here is either change the variable name in the inner loop or change the variable name in the outer loop.

It's mon for nested loops, to use a different loop variable, such as while the outer loop uses var i=0; the inner loop uses var j=0;.

The posted code, the 2nd var ind is duplicated, because it shares the same variable scope of the 1st var.

Post a comment

comment list (0)

  1. No comments so far