常用JS工具集合,持续更新 ...
//事件处理(2012.2.15 / 2012/3/27) var EventUtil = { addHandler:function(ele,type,handler){ //绑定事件 var list = {a:["addEventListener","attachEvent"],r:["removeEventListener","datachEvent"]}; var name = arguments[3]===true ? list["r"]:list["a"]; if(ele[name[0]]){ ele[name[0]](type,handler,false) }else if(ele.attachEvent){ ele[name[1]]("on"+type,handler); }else{ var handler = arguments[3]===true ? handler : null; ele["on"+type] = handler; } }, removeHandler:function(ele,type,handler){ //删除绑定 this.addHandler(ele,type,handler,true); }, getEvent:function(event){ //获取事件对象 return event ? event:window.event; }, getTarget:function(event){ //获取正在处理发生事件的对象 return event.target || event.srcElement; }, stopPropagation:function(event){ //阻止冒泡 if(event.stopPropagation){ event.stopPropagation(); }else{ event.cancelBubble = true; } }, preventDefalut:function(event){ //阻止默认行为 if(event.preventDefalut){ event.preventDefault(); }else{ event.returnValue = false; } }, getRelatedTarget:function(event){ //获取当前目标(mouseover Or mouseout)的时候有效 if(event.getRelatedTarget){ return event.getRelatedTarget; }else if(event.toElement){ return event.toElement; }else if(event.fromElement){ return event.fromElement; }else{ return null; } }, addLoad:function(fun){ //window.onload处理 var nowLoad = window.onload; if(typeof nowLoad!="function"){ window.onload = fun; }else{ window.onload = function(){ nowLoad(); fun(); }; }; } }; //获取对象 function g(id){ if(!id)return; if(typeof id == "string"){ return document.getElementById(id); }; return id; }; //创建容器 function createEle(ele,css,father){ ele = ele || "div"; var obj = document.createElement(ele); if(css){ setStyle(ele,css); }; father = father || document.body; father.appendChild(obj); }; //设置样式 function setStyle(ele,css){ if(!ele)return; var x,y,z,m; z = ele; x = z.style; for(var s in css){ m = css[s]; y = m + "px"; switch(s){ case "w" : x.width = y;break; case "h" : x.height = y;break; case "l" : x.left = y;break; case "t" : x.top = y;break; case "z" : x.zIndex = m;break; case "a" : x.position = m;break; case "d" : x.display = m;break; case "i" : z.innerHTML = m;break; case "bg": x.background = x;break; case "b" : x.border = x ;break; case "d" : x.display = x ;break; case "m" : x.margin = x;break; case "p" : x.padding = x;break; default : x[s] = m; break ; }; }; }; //获取窗口高宽 function getW(){ var client_h,client_w,scrollTop; client_h = document.documentElement.clientHeight || document.body.clientHeight; client_w = document.documentElement.clientWidth || document.body.clientWidth; scrollTop = document.documentElement.scrollTop || document.body.scrollTop; return o = {w:client_w,h:client_h,s:scrollTop}; } //获取CSS function getStyles(ele) { var style; if (document.defaultView && document.defaultView.getComputedStyle) style = document.defaultView.getComputedStyle(ele, null); else style = ele.currentStyle; return style; }; //随即数 function getRandom(upper,lower){ return Math.floor(Math.random() * (upper - lower + 1) + lower); }; //getElePosition function getElePosition(ele){ if(!ele)return; var left = 0,top = 0; if("getBoundingClientRect" in document.documentElement){ var box = ele.getBoundingClientRect(), doc = ele.ownerDocument, body = doc.body, docElem = doc.documentElement, clientTop = doc.clientTop || body.clientTop || 0, clientLeft = doc.clientLeft || body.clientLeft || 0 , left = box.left + (self.pageXOffset || docElem && docElem.scrollLeft || body.scrollTop) - clientLeft, top = box.top + (self.pageYOffset || docElem && docElem.scrollTop || body.scrollTop) - clientTop; }else{ do{ top+=ele.offsetTop, left+=ele.offsetLeft, ele = elel.offsetParent; }while(ele) }; return {left:left,top:top} }; //getId / getClass / getEle / addClass / removeClass var tool={ $:function(id){ return typeof id==="object"?id:document.getElementById(id) }, $$:function(tagName,oParent){ return(oParent||document).getElementsByTagName(tagName) }, $$$:function(className,tagName,elem){ var i=0,aClass=[], reClass=new RegExp("(^|\\s)"+className+"(\\s|$)"), aElement=tool.$$(tagName||"*",elem||document); for(i=0;i
(function () { //brwoser browser = navigator.userAgent.toLowerCase(); isAndroid = find(browser, "android"); isIphone = find(browser, "iphone"); isIpad = find(browser, "ipad"); isTouch = isIphone || isIpad; })(); function find(a, b) { return a.indexOf(b) + 1; }; function getMS() { var z = new Date(); return (z.getMinutes() * 60 + z.getSeconds()) * 1000 + z.getMilliseconds() };