原生js :removeClass和addClass

 1 function removeClass(obj, aClass) {
 2     var re = new RegExp('\\b' + aClass + '\\b');
 3     if (obj.className != '') {
 4 
 5         if (re.test(obj.className)) {
 6             var arr = obj.className.split(' ');
 7             for (var i = 0; i < arr.length; i++) {
 8                 if (arr[0] == aClass && arr[arr.length - 1] == aClass) {
 9                     obj.className = '';
10                 } else if (arr[i] == aClass) {
11                     arr.splice(i, 1);
12                     obj.className = arr.join(' ');
13 
14                 }
15             }
16         } else {
17             return;
18         }
19     }
20     removeClass(obj, aClass);
21 }
removeClass
 1 addClass = function (obj, aClass) {
 2     var re = new RegExp('\\b' + aClass + '\\b');
 3 
 4     if (obj.className) {
 5         if (!re.test(obj.className)) {
 6             obj.className += ' ' + aClass;
 7         }
 8     } else {
 9         obj.className = aClass;
10     }
11     return this;
12 }
addClass
 1 function removeClass(obj, aClass) {
 2     var re = new RegExp('\\b' + aClass + '\\b');
 3     if (obj.className != '') {
 4         if (re.test(obj.className)) {
 5             var arr = obj.className.split(' ');
 6             for (var i = 0; i < arr.length; i++) {
 7                 if (arr[0] == aClass && arr[arr.length - 1] == aClass) {
 8                     obj.className = '';
 9                 } else if (arr[i] == aClass) {
10                     arr.splice(i, 1);
11                     obj.className = arr.join(' ');
12                 }
13             }
14         }
15     }
16 }
removeClass

 

posted @ 2017-10-06 15:09 左转右转 阅读(...) 评论(...) 编辑 收藏