原生js :removeClass和addClass

 function removeClass(obj, aClass) {
var re = new RegExp('\\b' + aClass + '\\b');
if (obj.className != '') { if (re.test(obj.className)) {
var arr = obj.className.split(' ');
for (var i = 0; i < arr.length; i++) {
if (arr[0] == aClass && arr[arr.length - 1] == aClass) {
obj.className = '';
} else if (arr[i] == aClass) {
arr.splice(i, 1);
obj.className = arr.join(' '); }
}
} else {
return;
}
}
removeClass(obj, aClass);
}

removeClass

 addClass = function (obj, aClass) {
var re = new RegExp('\\b' + aClass + '\\b'); if (obj.className) {
if (!re.test(obj.className)) {
obj.className += ' ' + aClass;
}
} else {
obj.className = aClass;
}
return this;
}

addClass

 function removeClass(obj, aClass) {
var re = new RegExp('\\b' + aClass + '\\b');
if (obj.className != '') {
if (re.test(obj.className)) {
var arr = obj.className.split(' ');
for (var i = 0; i < arr.length; i++) {
if (arr[0] == aClass && arr[arr.length - 1] == aClass) {
obj.className = '';
} else if (arr[i] == aClass) {
arr.splice(i, 1);
obj.className = arr.join(' ');
}
}
}
}
}

removeClass

上一篇:Java基础系列--final、finally关键字


下一篇:理解JavaScript模仿块作用域