封装removeClass()
1 <div class="box haha xixi">123</div> 2 <script> 3 function removeClass(element,classNa){ 4 var arr=new Array(); 5 var classArr=element.className.split(" "); 6 element.className=""; 7 for(var i=0;i<classArr.length;i++){ 8 if(classArr[i]===classNa){ 9 classArr.splice(i,1); 10 } 11 } 12 element.className=classArr.join(" "); 13 } 14 var box=document.getElementsByTagName("div")[0]; 15 removeClass(box,"box")
1 var div=document.getElementsByTagName("div")[0]; 2 function addClass(element,classNa){ 3 div.className+=" "+classNa; 4 } 5 addClass(div,"lala")