Js实现检测、添加、移除样式(className)

// 说明:添加、移除、检测 className 
 
function hasClass(element, className) { 
    var reg = new RegExp('(\\s|^)'+className+'(\\s|$)'); 
    return element.className.match(reg); 
} 
 
function addClass(element, className) { 
    if (!this.hasClass(element, className)) 
    { 
        element.className += " "+className; 
    } 
} 
 
function removeClass(element, className) { 
    if (hasClass(element, className)) { 
        var reg = new RegExp('(\\s|^)'+className+'(\\s|$)'); 
        element.className = element.className.replace(reg,' '); 
    } 
}

你可能感兴趣的:(element)