<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>
【DOM编程艺术】getElementsByClassName
时间: 2024-10-25 07:57:34