-
Notifications
You must be signed in to change notification settings - Fork 0
/
dom-class.js
113 lines (112 loc) · 3.45 KB
/
dom-class.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
"use strict";
/**
* 通过calss获取DOM元素
* author:JM
* Released on: August 11, 2016
* @param {Object} [arguments[0]=document] - 上级DOM元素
* @param {String} arguments[1] - 想要获取DOM元素的class
* */
function getByClass(){
var oParent = arguments.length == 1?document:arguments[0];
var sClass = arguments.length == 1?arguments[0]:arguments[1];
if(document.getElementsByClassName){
return oParent.getElementsByClassName(sClass);
}else{
var aTmp = oParent.getElementsByTagName('*');
var aRes=[];
var arr =[];
for(var i=0;i<aTmp.length;i++){
arr = aTmp[i].className.split(' ');
for (var j=0;j<arr.length;j++){
if(arr[j] == sClass){
aRes.push(aTmp[i]);
}
}
}
return aRes;
}
}
/**
* DOM元素增加class
* author:JM
* Released on: August 12, 2016
* @param {Object} obj - 想要操作的DOM元素
* @param {String} class1, class2, ... - 想要添加到DOM元素的class
* info:并没有添加去重功能,考虑到class去重影响性能且浏览器解析时并无太大影响
* */
function addClass(obj,class1){
var aClass = [];
for(var i=1;i<arguments.length;i++){
aClass.push(arguments[i]);
}
if("classList" in document.documentElement && !(!!window.ActiveXObject || "ActiveXObject" in window)){
obj.classList.add.apply(obj.classList,aClass);
}else{
var sClass = aClass.join(' ');
obj.className += (obj.className != '')?' '+sClass:sClass;
}
}
/**
* DOM元素删除class
* author:JM
* Released on: August 12, 2016
* @param {Object} obj - 想要操作的DOM元素
* @param {String} class1, class2, ... - 想要删除DOM元素的class
* */
function removeClass(obj,class1){
var aClass = [];
for(var i=1;i<arguments.length;i++){
aClass.push(arguments[i]);
}
if("classList" in document.documentElement && !(!!window.ActiveXObject || "ActiveXObject" in window)){
obj.classList.remove.apply(obj.classList,aClass);
}else{
var sObjClass = ' '+obj.className+' ';
for(var j=0;j<aClass.length;j++){
var reg = new RegExp(' '+aClass[j]+' ','g');
sObjClass = sObjClass.replace(reg,' ');
}
sObjClass = sObjClass.replace(/(\s+)/gi, ' ');
sObjClass = sObjClass.replace(/(^\s+)|(\s+$)/g, '');
obj.className = sObjClass;
}
}
/**
* 判断DOM元素是否存在某个class
* author:JM
* Released on: August 12, 2016
* @param {Object} obj - 想要操作的DOM元素
* @param {String} sclass - 想要删除DOM元素的class
* */
function hasClass(obj, sclass){
if("classList" in document.documentElement){
return obj.classList.contains(sclass);
}else{
var sObjClass = obj.className;
var aObjClass = sObjClass.split(/\s+/);
for(var i in aObjClass) {
if(aObjClass[i] == sclass) {
return true;
}
}
return false;
}
}
/**
* 在元素中切换类名
* author:JM
* Released on: August 12, 2016
* @param {Object} obj - 想要操作的DOM元素
* @param {String} sclass - 想要删除DOM元素的class
* */
function toggleClass(obj,sclass){
if("classList" in document.documentElement){
return obj.classList.toggle(sclass);
}else{
if(hasClass(obj,sclass)){
removeClass(obj,sclass);
}else{
addClass(obj,sclass);
}
}
}