1. 程式人生 > >js原生新增clsss

js原生新增clsss

	
function hasClass(element,Hclass){ //判斷是否有 '要新增的class'
		var patt=new RegExp(Hclass)
		return patt.test(element.className)
	}
	function addClass(element,Hclass){ //追加class
	  if(!hasClass(element,Hclass)){
	      element.className+=' '+Hclass;
			// console.log(element.className)
		}
	}
	function removeClass(element,Hclass){ //追加class
	  if(hasClass(element,Hclass)){ //如果有 '要刪除的class'
			var re = element.className
			var bb = 'aaabbb'
	    	re= re.replace(Hclass,'');
			element.className=re
		}
	}