Js工具函数收集

Posted by Turingdo Studio on June 15, 2017

1. className 相关方法

function hasClass(obj, classname) {
    reg = new RegExp("\\b" + classname + "\\b");
    //reg=/((\s)*(\w)*)*tra_box((\s)*(\w)*)*/;
    return reg.test(obj.className);
    //return obj.className.match(reg);
}
 
function addClass(obj, classname) {
 
    if (hasClass(obj, classname)) {
        return;
    } else {
        obj.className += " " + classname;
    }
 
}
 
function removeClass(obj, classname) {
    if (hasClass(obj, classname)) {
        obj.className = obj.className.replace(classname, "");
    } else {
        return;
    }
 
}