function getElementsByClassName( parent,tag,className ){
var aEls = parent.getElementsByTagName(tag);
var arr = [];
for (var i = 0; i < aEls.length; i++) {
var aClassName = aEls[i].className.split(‘ ‘);
for (var j = 0; j < aClassName.length; j++) {
if(aClassName[j] == className){
arr.push(aEls[i]);
//如果className里面包含‘box‘ 则跳出循环
break; //防止一个元素出现多次相同的class被添加多次
}
};
};
return arr;
}
时间: 2024-10-22 00:16:32