简体   繁体   中英

How to turn html tags into an array in javascript

I am relatively new to JavaScript but I have a lot of background in java. So, I am wondering how I could loop through every single tag in a special div. Like a for loop. This is what I have already:

var allPeople = document.getElementsByClassName('person').toArray;

console.log(allPeople.length);

for(var i = 0; i < allPeople.length; i++) {
    console.log(document.getElementById("#main > .person > #inside > #span"));
    document.getElementById("#main > .person > #inside > #left > li > #normal > #kd").innerHTML = 1;
}

I think the problem is with the 'document.getElementsByClassName('person').toArray;' part. Any other recommendations?

remove the .toArray in line 1

the function getElementsByClassName() returns an array, you do not need to convert it to one.

如果您使用的是es6

[ ...document.getElementsByClassName('person')].forEach( el => { //do something })

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM