For loop for HTMLCollection elements

前端 未结 12 1079
挽巷
挽巷 2020-11-22 04:20

I\'m trying to set get id of all elements in an HTMLCollectionOf. I wrote the following code:

var list = document.getElementsByClassName(\"event         


        
12条回答
  •  既然无缘
    2020-11-22 04:45

    Easy workaround that I always use

    let list = document.getElementsByClassName("events");
    let listArr = Array.from(list)
    

    After this you can run any desired Array methods on the selection

    listArr.map(item => console.log(item.id))
    listArr.forEach(item => console.log(item.id))
    listArr.reverse()
    

提交回复
热议问题