js原生实现getElementsByClassName

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){}

}

 

转载于:https://www.cnblogs.com/dsphper/p/4303313.html

你可能感兴趣的:(js原生实现getElementsByClassName)