<!DOCTYPE html>
<html>
<head>
<title>classNameSet</title>
</head>
<script type="text/javascript">
function getByClass(oParent,sClass)
{
var sClassName=[];
var aEle=oParent.getElementsByTagName('*');
for (var i = 0; i < aEle.length; i++)
{
if (aEle[i].className==sClass)
{
sClassName.push(aEle[i]);
}
}
return sClassName;
}
window.onload=function()
{
var oUl=document.getElementById('ul1');
var aBox=getByClass(oUl,'box');
// alert(aBox);
for (var i = 0; i < aBox.length; i++) {
aBox[i].style.background='red';
}
}
</script>
<body>
<ul id="ul1">
<li class="box"></li>
<li class="box"></li>
<li></li>
<li class="box"></li>
<li></li>
</ul>
</body>
</html>
网友评论