2021-09-04 19:29:28 +08:00
|
|
|
export const hasClass = function (obj, cls) {
|
2020-08-13 15:18:26 +08:00
|
|
|
return obj.className.match(new RegExp('(\\s|^)' + cls + '(\\s|$)'))
|
|
|
|
}
|
|
|
|
|
2021-09-04 19:29:28 +08:00
|
|
|
export const addClass = function (obj, cls) {
|
2020-08-13 15:18:26 +08:00
|
|
|
if (!hasClass(obj, cls)) obj.className += ' ' + cls
|
|
|
|
}
|
|
|
|
|
2021-09-04 19:29:28 +08:00
|
|
|
export const removeClass = function (obj, cls) {
|
2020-08-13 15:18:26 +08:00
|
|
|
if (hasClass(obj, cls)) {
|
|
|
|
const reg = new RegExp('(\\s|^)' + cls + '(\\s|$)')
|
|
|
|
obj.className = obj.className.replace(reg, ' ')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-04 19:29:28 +08:00
|
|
|
export const toggleClass = function (obj, cls) {
|
2020-08-13 15:18:26 +08:00
|
|
|
if (hasClass(obj, cls)) {
|
|
|
|
removeClass(obj, cls)
|
|
|
|
} else {
|
|
|
|
addClass(obj, cls)
|
|
|
|
}
|
|
|
|
}
|