首页 > 代码库 > 【DOM编程艺术】getElementsByClassName

【DOM编程艺术】getElementsByClassName

<div id=‘shop‘>
    <div class=‘a1 a2‘>111</div>
    <div class=‘a1 a3‘>222</div>
    <div class=‘a2 a3‘>333</div>
</div>
<script type="text/javascript">
function getElementsByClassName(node,classname){
    if(node.getElementsByClassName){ 
        return node.getElementsByClassName(classname);
    }else{
        var elem = node.getElementsByTagName(‘*‘);
        var result=[];
        for(var i =0; i<elem.length;i++){
            if(elem[i].className.indexOf(classname) != -1){
                result[result.length]=elem[i];
            }
        }
        return result;
    }

}
var shoping = document.getElementById(‘shop‘);
var thing = getElementsByClassName(shoping,‘a1‘);
console.log(thing);
</script>