<!doctype html>

<html>

<head>

<meta charset="utf-8">

<meta name="author" content="前端工程师——和派孔明" />

<meta name="copyright" content="前端工程师——和派孔明" />

<title>getElementsByClassName IE低不兼容解决</title>

<style>

* {

margin: 0;

padding: 0;

list-style: none;

font-family: "微软雅黑", "张海山锐线体简"

}

div {

height: 50px;

background: #ccc;

margin: 10px;

}

</style>

<script>

window.οnlοad=function(){

var aBox = getByClass(document,'box');


for(var i=0;i<aBox.length;i++){

aBox[i].style.background='red';


}

};



function getByClass(oParent,sClass){
var aEle = oParent.getElementsByTagName('*');//父级获取下面所有
var result=[];//准备空数组
for(var i=0;i<aEle.length;i++){
//临时数组,装切出来的单个aEle 元素的class
var tmpArr = aEle[i].className.split(' ');//[box a b c d e f g]
for(var j=0;j<tmpArr.length;j++){
if(tmpArr[j] == sClass){//数组里每一个字符和传入的box对比
result.push(aEle[i]);
break;
}
}
}
return result;
}

</script>

</head>



<body>



<div class="box a b c d e f g">box a b c d e f g</div>

<div class=""></div>

<p class="active box">active box</p>

<div></div>

<div class="a_box_b">a_box_b</div>

</body>

</html>