js原生实现getElementsByClassName

时间:2021-02-09 16:05:32
document.getElementsByClassName = function(name){
    try{
        var tag = document.getElementsByTagName("*") || document.all;
        var array = [];
        for (var i = 0; i < tag.length; i++) {
            if(tag[i].className == name){
                array.push(tag[i]);
            };
        };
        if(array.length >= 1){
            return array[0];
        }
        return array;
    }catch(e){}

}