首页 > 代码库 > JavaScript实现获取DOM的class

JavaScript实现获取DOM的class

JavaScript实现获取DOM的class
<!DOCTYPE html><html lang="en"><head> <meta charset="utf-8"> <script> window.onload = function () { var d = getClass(document,‘cls‘); alert(d.innerHTML="555") } function getClass(node,classname) { if(node.getElementsByClassName) { //如果存在该标签 就返回 return node.getElementsByClassName(classname); } else{ var defualt = [], elems = node.getElementsByTagName(‘*‘); for (var i = 0 ;i < elems.length; i++) {//遍历所有标签 if(elems[i].className.indexOf(classname) != -1){ 查找相应标签 defualt[defualt.length]=elems[i]; } } return defualt; } } </script> </head><body> <div class="cls"></div></body></html>

 

JavaScript实现获取DOM的class