js操作元素className

Base.prototype.addClass = function (className) {
	for (var i = 0; i < this.elements.length; i ++) {
		if (!this.elements[i].className.match(new RegExp('(\\s|^)' + className + '(\\s|$)'))){
			  this.elements[i].className += ' ' + className;
		}
	}
	return this;
}
//移除CLASS
Base.prototype.removeClass = function (className) {
	for (var i = 0; i < this.elements.length; i ++) {
		if (this.elements[i].className.match(new RegExp('(\\s|^)' + className + '(\\s|$)'))){
			this.elements[i].className = this.elements[i].className.
			replace(new RegExp('(\\s|^)' + className + '(\\s|$)'), ' ');
		}
	}
	return this;
}

 

posted @ 2016-04-25 00:47  天桥残局  阅读(311)  评论(0编辑  收藏  举报