JS: iterating over result of getElementsByClassName using Array.forEach

前端 未结 11 1918
伪装坚强ぢ
伪装坚强ぢ 2020-11-22 06:42

I want to iterate over some DOM elements, I\'m doing this:

document.getElementsByClassName( \"myclass\" ).forEach( function(element, index, array) {
  //do s         


        
11条回答
  •  无人及你
    2020-11-22 07:21

    Or you can use querySelectorAll which returns NodeList:

    document.querySelectorAll('.myclass').forEach(...)
    

    Supported by modern browsers (including Edge, but not IE):
    Can I use querySelectorAll
    NodeList.prototype.forEach()

    MDN: Document.querySelectorAll()

提交回复
热议问题