Return loop getElementsByClassName not work [closed]

You could go the whole hog and extend the HTMLcollection prototype:

function className(cls){
    return document.getElementsByClassName(cls);//a HTMLCollection
}
//extend the HTMLCollection prototype
HTMLCollection.prototype.style = function(newStyles) {
    for (var i = 0; i < this.length; i++) {
        for (var key in newStyles) {
          if (newStyles.hasOwnProperty(key)) {
              this[i].style[key] = newStyles[key];
          }
        }
    }
    return this;
};

//example use
className('source').style({display:'block', color:'red'});

Not necessarily something I’d recommend but here’s the demo

Browse More Popular Posts

Leave a Comment