<script type="text/javascript">
window.onload=function(){
var len1= getByClass('on','list');
alert(len1.length); // 结果等于2为正确
var len2= getByClass('on');
alert(len1.length); // 结果等于3为正确
}
function getByClass(clsName, parent){
//定义函数getByClass()实现获取document或指定父元素下所有class为on的元素
var oParent=parent?document.getElementById(parent):document;
var arr=new Array();
var oElements=oParent.getElementsByTagName("*");
for(var i=0;i<oElements.length;i++){
if(oElements[i].className == clsName){
arr.push(oElements[i]);
}
}
return arr;
}
</script>