码迷,mamicode.com
首页 > 编程语言 > 详细

JavaScript实现获取DOM的class

时间:2015-01-05 14:29:44      阅读:110      评论:0      收藏:0      [点我收藏+]

标签:

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

标签:

原文地址:http://www.cnblogs.com/alancz/p/4203394.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!