【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>

 

posted @ 2014-05-19 10:50  Western Journey  阅读(141)  评论(0编辑  收藏  举报