<html>
<head></head>
<body>
<ul id="ul1">
<li></li>
<li></li>
<li></li>
<li class="box"></li>
<li></li>
<li class="box"></li>.
<li class="box"></li>
<li></li>
<li></li>
<li></li>
</ul>
</body>
</html>
function getByClass(oParent, sClass) {
//获取所有标签
var aEle = oParent.getElementsByTagName('*');
//存放所有被选中的属性
var aResult = [];
//循环所有被选出来的元素
for (var i = 0; i < aEle.length; i++) {
//class为传进的sclass
if (aEle[i].className == sClass) {
//借点存入aResult
aResult.push(aEle[i]);
}
}
return aResult;
}
window.onload = function() {
//获取名字ul1的ID
var oUl = document.getElementById('ul1');
//传参
var aBox = getByClass(oUl, 'box');
//循环class
for (var i = 0; i < aBox.length; i++) {
aBox[i].style.background = 'red';
}
}