-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathutils.ts
37 lines (34 loc) · 986 Bytes
/
utils.ts
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
export function addClass(element: Element, className: string) {
if (element.classList) {
element.classList.add(className)
} else {
element.className += ` ${className}`
}
}
export function removeClass(element: Element, className: string) {
if (element.classList) {
element.classList.remove(className)
} else {
const re = new RegExp(
"(^|\\b)" + className.split(" ").join("|") + "(\\b|$)",
"gi"
)
element.className = element.className.replace(re, " ")
}
}
export function addEventListener(element: Element, event: string, cb: any) {
if (element.addEventListener) {
element.addEventListener(event, cb)
} else {
(element as any).attachEvent(`on${event}`, () => {
cb.call(element)
})
}
}
export function removeEventListener(element: Element, event: string, cb: any) {
if (element.removeEventListener) {
element.removeEventListener(event, cb)
} else {
(element as any).detachEvent(`on${event}`, cb)
}
}