简单的javascript代码getclass函数

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script>
window.onload=function(){
function getclass(cName,eParent){
var eParent = eParent ? eParent : window.document.body; //默认值
classList=eParent.getElementsByTagName('*');
var arrElements=new Array;
var reg=new RegExp('\\b'+cName+'\\b');
for(i=0;i<classList.length;i++){
if (reg.test(classList[i].className)){
           	 arrElements.push(classList[i]);
} 
}
return arrElements;
}
alert(getclass("t").length);
}
</script>
</head>
<body>
<div class="t1 t t2">11</div>
<div class="tt t ttt">22</div>
</body>
</html>

你可能感兴趣的:(getClass)