For loop for HTMLCollection elements

前端 未结 12 1077
挽巷
挽巷 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:29

    You want to change it to

    var list= document.getElementsByClassName("events");
    console.log(list[0].id); //first console output
    for (key in list){
        console.log(list[key].id); //second console output
    }
    
    0 讨论(0)
  • 2020-11-22 04:31

    you can add this two lines:

    HTMLCollection.prototype.forEach = Array.prototype.forEach;
    NodeList.prototype.forEach = Array.prototype.forEach;
    

    HTMLCollection is return by getElementsByClassName and getElementsByTagName

    NodeList is return by querySelectorAll

    Like this you can do a forEach:

    var selections = document.getElementsByClassName('myClass');
    
    /* alternative :
    var selections = document.querySelectorAll('.myClass');
    */
    
    selections.forEach(function(element, i){
    //do your stuffs
    });
    
    0 讨论(0)
  • 2020-11-22 04:32

    Alternative to Array.from is to use Array.prototype.forEach.call

    forEach: Array.prototype.forEach.call(htmlCollection, i => { console.log(i) });

    map: Array.prototype.map.call(htmlCollection, i => { console.log(i) });

    ect...

    0 讨论(0)
  • 2020-11-22 04:33

    As of March 2016, in Chrome 49.0, for...of works for HTMLCollection:

    this.headers = this.getElementsByTagName("header");
    
    for (var header of this.headers) {
        console.log(header); 
    }
    

    See here the documentation.

    But it only works if you apply the following workaround before using the for...of:

    HTMLCollection.prototype[Symbol.iterator] = Array.prototype[Symbol.iterator];
    

    The same is necessary for using for...of with NodeList:

    NamedNodeMap.prototype[Symbol.iterator] = Array.prototype[Symbol.iterator];
    

    I believe/hope for...of will soon work without the above workaround. The open issue is here:

    https://bugs.chromium.org/p/chromium/issues/detail?id=401699

    Update: See Expenzor's comment below: This has been fixed as of April 2016. You don't need to add HTMLCollection.prototype[Symbol.iterator] = Array.prototype[Symbol.iterator]; to iterate over an HTMLCollection with for...of

    0 讨论(0)
  • 2020-11-22 04:41

    In ES6, you could do something like [...collection], or Array.from(collection),

    let someCollection = document.querySelectorAll(someSelector)
    [...someCollection].forEach(someFn) 
    //or
    Array.from(collection).forEach(someFn)
    

    Eg:-

        navDoms = document.getElementsByClassName('nav-container');
        Array.from(navDoms).forEach(function(navDom){
         //implement function operations
        });
    
    0 讨论(0)
  • 2020-11-22 04:42

    On Edge

    if(!NodeList.prototype.forEach) {
      NodeList.prototype.forEach = function(fn, scope) {
        for(var i = 0, len = this.length; i < len; ++i) {
          fn.call(scope, this[i], i, this);
        }
      }
    }
    
    0 讨论(0)
提交回复
热议问题