addClass和removeClass函数

  function addClass1(element,value){
        if(!element.className){
            element.className = value;
        }else{
            newClassName = element.className;
            newClassName += " ";
            newClassName += value;
            element.className =newClassName;
        };
    };
    function addClass2(ele,className){
        ele.className += " " + className;
    };
    function addClass3(node,className){
        return node.className += " " + className;
    };

  

   function removeClass2(ele,className){
        var tmpClassName = ele.className;
        ele.className = null;
        ele.className = tmpClassName.split(new RegExp(" " + className + "|" + className + " " + "|" + "^" + className + "$","ig")).join("");
    };
    function removeClass3(node,className){
        eles = node.className.split(/\s+/);//先将已有的class放进数组
        for(var i = 0; i < eles.length; i++){
            if(eles[i] == className){
                eles.splice(i,1);//再遍历删除指定的class
            };
        };
        node.className = eles.join("");//最后将新的数组用空格隔开重新添加回className
        return node;
    };

  

posted @ 2014-11-24 11:02  Joerias  阅读(334)  评论(0编辑  收藏  举报