首页 > 代码库 > 封装函数--->切换class

封装函数--->切换class

        var obj={};        obj.className=‘a b c d active‘;        function toggle(obj,className) {            var str=obj.className;            var arr=str.split(" ");            var onOff=true;            for (var i = 0; i < arr.length; i++) {                if(arr[i]===className){                    onOff=false;                    arr.splice(i,1);                    i--;                }            }            if (onOff) {                arr.push(className);            }            obj.className=arr.join(" ");            return obj.className;        }        function addClass(obj,className) {            var str=obj.className;            var arr=str.split(" ");            for (var i = 0; i < arr.length; i++) {                if(arr[i]===className){                    return obj.className;                }            }            arr.push(className);            obj.className=arr.join(" ");            return obj.className;        }        function removeClass(obj,className) {            var str=obj.className;            var arr=str.split(" ");            for (var i = 0; i < arr.length; i++) {                if(arr[i]===className){                    arr.splice(i,1);                    i--;                }            }            obj.className=arr.join(" ");            return obj.className;        }

 

封装函数--->切换class