js - class 操作
js - class 操作
// 添加
function addClass(dom, classNameString = '') {
if (!dom.className.length) dom.className = classNameString
else if (dom.className.indexOf(classNameString) == -1) dom.className += ' ' + classNameString
}
// 移除
function removeClass(dom, classNameString = '') {
if (dom.className.length && dom.className.indexOf(classNameString) != -1) {
dom.className = dom.className
.split(' ')
.filter(v => v != classNameString)
.join(' ')
}
}
// 查询
function hasClass(dom, classNameString = '') {
return dom.className.indexOf(classNameString) == -1
}
Lee2