1 function getElementsByClassName(classStr,tagName,element){ 2 tagName = (tagName || '*'); 3 element = (element || document); 4 5 if(typeof tagName == 'object'){ 6 element = tagName; tagName = '*'; 7 } 8 9 if(document.getElementsByClassName){ 10 return element.getElementsByClassName(classStr); 11 }else{ 12 var nodes = element.getElementsByTagName(tagName), 13 ret = []; 14 for(i = 0; i < nodes.length; i++) { 15 if(hasClass(nodes[i],classStr)) ret.push(nodes[i]); 16 } 17 return ret; 18 } 19 20 function hasClass(tagStr,classStr){ 21 var arr=tagStr.className.split(/\s+/ ); //这个正则表达式是因为class可以有多个,判断是否包含 22 for (var i=0;i<arr.length;i++){ 23 if (arr[i]==classStr) return true; 24 } 25 return false; 26 }; 27 };