函数如下:
function getElementByClassName(cls,elm) {
var arrCls =[];
var seeElm = !mtLibs.IsNull(elm) ? elm : '*';
var rexCls = new RegExp('(^|\\\\s)' + cls + '(\\\\s|$)','i');
var lisElm = document.getElementsByTagName(seeElm);
for (var i=0; i<lisElm.length; i++ ) {
var evaCls = lisElm[i].className;
if(evaCls.length > 0 && (evaCls == cls || rexCls.test(evaCls))) {
arrCls.push(lisElm[i]);
}
}
return arrCls;
}
应用例子:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<div class='b'>dsd</div>
<script type="text/javascript">
function getElementByClassName(cls,elm) {
var arrCls =[];
var seeElm = !mtLibs.IsNull(elm) ? elm : '*';
var rexCls = new RegExp('(^|\\\\s)' + cls + '(\\\\s|$)','i');
var lisElm = document.getElementsByTagName(seeElm);
for (var i=0; i<lisElm.length; i++ ) {
var evaCls = lisElm[i].className;
if(evaCls.length > 0 && (evaCls == cls || rexCls.test(evaCls))) {
arrCls.push(lisElm[i]);
}
}
return arrCls;
}
document.write(getElementByClassName('b'));
</script>
</body>
</html>
输出:dsd
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请
点击举报。