dom.js
1023 Bytes
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
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.hasClass = hasClass;
exports.addClass = addClass;
exports.removeClass = removeClass;
function hasClass(el, cls) {
if (!el || !cls) return false;
if (cls.indexOf(' ') !== -1) throw new Error('className should not contain space.');
if (el.classList) {
return el.classList.contains(cls);
} else {
return (' ' + el.className + ' ').indexOf(' ' + cls + ' ') > -1;
}
};
function addClass(el, cls) {
if (!el || !cls) return;
if (el.classList) {
el.classList.add(cls);
} else {
var clsArr = el.className.split(" ");
if (clsArr.indexOf(cls) === -1) {
el.className += " " + cls;
}
}
};
function removeClass(el, cls) {
if (!el || !cls) return;
if (el.classList) {
el.classList.remove(cls);
} else {
var reg = new RegExp('(\\s|^)' + cls + '(\\s|$)');
el.className = el.className.replace(reg, ' ');
}
};