兼容IE的getElementsByClassName()方法
兼容IE8及以下的getElementsByClassName()方法
1 2 function getElementsByClassName = function(className, tagName) { 3 //如果浏览器支持getElementsByClassName()就直接返回 4 if(document.getElementsByClassName){ 5 return document.getElementsByClassName(className) 6 } 7 //否则遍历document文档指定的标签名集合 8 var children = document.getElementsByTagName(tagName); 9 //此数组用于保存遍历后得到的class元素 10 var elements = new Array(); 11 12 for (var i = 0; i < children.length; i++) { 13 var child = children[i]; 14 var classNames = child.className.split(' '); //分割多个class元素 15 for (var j = 0; j < classNames.length; j++) { 16 if (classNames[j] == className) { 17 elements.push(child); 18 break; 19 } 20 } 21 } 22 return elements; 23 };