1 function getByClass(obj,sClass){ 2 if(obj.getElementsByClassName){ 3 return obj.getElementsByClassName(sClass); 4 }else{ 5 var arr=[]; 6 //var reg=/sClass/; 7 var reg=new RegExp('\b'+sClass+'\b'); 8 var aEle=obj.getElementsByTagName('*'); 9 10 for(var i=0; i<aEle.length; i++){ 11 if(reg.test(aEle[i].className)){ 12 arr.push(aEle[i]); 13 } 14 } 15 return arr; 16 } 17 } 18 19 function hasClass(obj,sClass){ 20 var reg=new RegExp('\b'+sClass+'\b'); 21 return reg.test(obj.className); 22 } 23 24 function addClass(obj,sClass){ 25 if(obj.className){ 26 if(!hasClass(obj,sClass)){ 27 obj.className+=' '+sClass; 28 } 29 }else{ 30 obj.className=sClass; 31 } 32 } 33 34 function removeClass(obj,sClass){ 35 var reg=new RegExp('\b'+sClass+'\b','g'); 36 if(hasClass(obj,sClass)){ 37 obj.className=obj.className.replace(reg,'').replace(/^s+|s+$/g,'').replace(/s+/g,' '); 38 } 39 } 40 41 function toggleClass(obj,sClass){ 42 if(hasClass(obj,sClass)){ 43 removeClass(obj,sClass); 44 }else{ 45 addClass(obj,sClass); 46 } 47 }