常用函數(shù)歸檔
當(dāng)前位置:點(diǎn)晴教程→知識(shí)管理交流
→『 技術(shù)文檔交流 』
/**************************************************
* * sl是scriptlover的簡(jiǎn)稱 ,源自http://www.scriptlover.com(我的個(gè)人網(wǎng)站) * * 該文件主要將常用的函數(shù),功能做一下封裝,以便于精煉自己的程序 * * 2008-11-27 * ************************************************** * * msn:danxinju@hotmail.com * * author:淡新舉 ***************************************************/ var sl = window.sl = { /* ** <summary> ** 添加事件監(jiān)聽(tīng) ** </summary> ** <param name="target">監(jiān)聽(tīng)對(duì)象</param> ** <param name="eventType">事件類型</param> ** <param name="handler">處理函數(shù)</param> */ addEventHandler:function(target, eventType, handler){ if (target.addEventListener) { target.addEventListener(eventType, handler, false); } else if (target.attachEvent) { target.attachEvent("on" + eventType, handler); } else { target["on" + eventType] = handler; } } , /* ** <summary> ** 構(gòu)建字符串對(duì)象 ** </summary> */ stringBuilder:function(){ this.arr = []; /*添加字符項(xiàng)目*/ this.push = function(item){ this.arr.push(item); }; /*清空字符串*/ this.clear = function(){ this.arr.length = 0; }; /*返回結(jié)果字符串*/ this.toString = function(){ return this.arr.join(""); }; } }; /* ** <summary> ** 獲取對(duì)象 ** </summary> ** <param name="selector">選擇參數(shù)</param> ** <param name="parentNode">父節(jié)點(diǎn)</param> */ sl.getElement = function(selector ,parentNode){ /* selector參數(shù)解釋(均支持以,隔開(kāi)的多重選擇) #)前綴根據(jù)id返回對(duì)象 =)根據(jù)屬性返回對(duì)象 )無(wú)符號(hào)則根據(jù)tagName返回對(duì)象 */ /*#)前綴根據(jù)id返回對(duì)象*/ var execId = function(selector){ var selector = selector.replace(/#| /g ,""); var arr = selector.split(",") ,rets = [] ,temp; for (var i=0;i<arr.length ;i++ ) { temp = document.getElementById(arr[i]); if (temp) { rets.push(temp); } } return (rets.length > 1)? rets : rets[0]; }; /*=)根據(jù)屬性返回對(duì)象*/ var execAttribute = function(selector ,parentNode){ var parentNode = parentNode? parentNode : document; var objects = parentNode.getElementsByTagName("*"); var arr = selector.split("=") ,rets = [] ,temp; for (var i=0;i<objects.length ;i++ ) { if (objects[i].getAttribute(arr[0]) == arr[1]) { rets.push(objects[i]); } } return (rets.length > 1)? rets : rets[0]; }; /*無(wú)符號(hào)則根據(jù)tagName返回對(duì)象*/ var execTagName = function(selector ,parentNode){ var parentNode = parentNode? parentNode : document; return parentNode.getElementsByTagName(selector); }; /*分流處理*/ if (selector.indexOf("#") != -1) { return execId(selector ,parentNode); } else if (selector.indexOf("=") != -1) { return execAttribute(selector ,parentNode); } else { return execTagName(selector ,parentNode); } }; /* ** <summary> ** ajax應(yīng)用 ** </summary> ** <param name="url">資源地址</param> ** <param name="callback">回調(diào)方法</param> ** <param name="method">請(qǐng)求方式</param> ** <param name="data">所發(fā)送數(shù)據(jù)</param> */ sl.ajax = function(url ,callback ,method ,data){ var method = method ? method : "get"; var data = data ? data : null; var url = (method == "get") ? (url +"?"+ data) : url; var http = window.XMLHttpRequest? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP"); var doGet = function(){ http.open(method ,url ,false); http.setRequestHeader("If-Modified-Since" , "0" ); http.send(null); callback(http.responseText); }; var doPost = function(){ http.open(method ,url ,false); http.setRequestHeader("content-length" ,data ?data.length : 0); http.setRequestHeader("Content-Type" ,"application/x-www-form-urlencoded"); http.send(data); callback(http.responseText); }; if (method == "post") { doPost(); } else { doGet(); } http = null; }; /* ** <summary> ** URl參數(shù)處理 ** </summary> ** <param name="arg">參數(shù)</param> */ sl.request = function(arg){ /*根據(jù)參數(shù)返回值*/ var queryString = function(arg){ var uri = window.location.search; var re = new RegExp(""+ arg +"\=([^\&\?]*)", "ig"); return ((uri.match(re))?(uri.match(re)[0].substr(arg.length+1)):null); }; /*不傳參數(shù)返回全部參數(shù)*/ var queryStrings = function(){ var uri = window.location.search; var re = /\w*\=([^\&\?]*)/ig; var retval=[]; while ((arr = re.exec(uri)) != null) retval.push(arr[0]); return retval; }; /*分流處理*/ if (arg) { return queryString(arg); } else { return queryStrings(); } }; /* ** <summary> ** Cookie處理 ** </summary> ** <param name="name">cookie鍵名</param> ** <param name="value">cookie鍵值</param> ** <param name="option">可選參數(shù)</param> */ sl.cookie = { /*設(shè)置cookie*/ setCookie:function(name, value, option){ var str = name +"="+ escape(value); if(option){ if(option.expireHours){ var d = new Date(); d.setTime(d.getTime()+option.expireHours*3600*1000); str += "; expires="+d.toGMTString(); } if(option.path) str += "; path="+option.path; if(option.domain) str += "; domain="+option.domain; if(option.secure) str += "; true"; } document.cookie = str; } , /*獲取cookie*/ getCookie:function(name){ var arr = document.cookie.split("; "); if(arr.length == 0) return ""; for(var i=0; i <arr.length; i++) { tmp = arr[i].split("="); if(tmp[0] == name) { return unescape(tmp[1]); } } return ""; } , /*刪除cookie*/ delCookie:function(name){ this.setCookie(name,"",{expireHours:-1}); } , /*獲取cookie長(zhǎng)度*/ getLength:function(){ return document.cookie.split("; ").length; } }; /* ** <summary> ** 獲取鼠標(biāo)位置 ** </summary> ** <param name="e">事件對(duì)象</param> */ sl.getMousePos = function (e){ var e = e?e : event; if (e.pageX || e.pageY) { return { x: e.pageX, y: e.pageY }; } else { return { x: e.clientX + document.documentElement.scrollLeft - document.body.clientLeft, y: e.clientY + document.documentElement.scrollTop - document.body.clientTop }; } }; /* ** <summary> ** 獲取對(duì)象位置 ** </summary> ** <param name="element">對(duì)象</param> */ sl.getElementPos = function(element){ var offsetTop = element.offsetTop; var offsetLeft = element.offsetLeft; while(element = element.offsetParent) { offsetTop += element.offsetTop; offsetLeft += element.offsetLeft; } return {x:offsetLeft ,y:offsetTop}; }; 該文章在 2010/8/13 18:54:00 編輯過(guò) |
關(guān)鍵字查詢
相關(guān)文章
正在查詢... |