这几天做的这个小项目,要求不能引用jQuery,全部用原生的javascript来写,突然发现都不知道改怎么写了。写了jQuery,把原生的都快忘记完了。把写出了的公用函数留下了,以后可能用得到。

  1 /**
  2 *@获取元素
  3 *@o-"#id"-根据id获取元素
  4 *@o-"#class"-根据class获取元素
  5 *@o-"#tagName"-根据元素名称获取元素
  6 */
  7 function $E(o){
  8     var regId =/^#/,regClass = /^./,keyword = "";
  9     if(regId.test(o)){
 10         keyword = o.replace("#","");
 11         return document.getElementById(keyword) || null;
 12     }
 13     if(regClass.test(o)){
 14         keyword = o.replace(".","");
 15         return document.getElementsByClassName(keyword) || null;
 16     }
 17     
 18     return document.getElementsByTagName(o) || null; 
 19 }
 20 /**
 21 *@扩展字符串去除空格
 22 */
 23 String.prototype.trim =function(){
 24     return  this.replace(/(^\s*)|(\s*$E)/g,'');
 25 };
 26 
 27 /***
 28 *常用工具函数
 29 */
 30 var Utils = {
 31   hasClass: function(element,classname){
 32         var reg = new RegExp('(\\s|^)'+classname+'(\\s|$)');   
 33         return element.className.match(reg);
 34   },
 35   
 36   addClass:function(element,classname){
 37         if(!this.hasClass(element,classname)){
 38             element.className += "" +classname;
 39         }
 40   },
 41   
 42   removeClass:function(element,classname){
 43         if(this.hasClass(element,classname)){
 44             var reg = new RegExp('(\\s|^)'+className+'(\\s|$)');      
 45             element.className = element.className.replace(reg,' ');   
 46         }
 47   },
 48   
 49   show:function(element){
 50         element.style.display = "block";
 51   },
 52   
 53   hide: function(element){
 54         element.style.display = "none";
 55   },
 56   
 57   //获取cookie解码后的值
 58   getCookieVal: function(offset){
 59         var endstr = document.cookie.indexOf (";", offset);
 60         if (endstr == -1) {endstr = document.cookie.length;}
 61         return unescape(document.cookie.substring(offset, endstr));
 62   },
 63   
 64   //获取cookie原始值
 65   getCookie: function(name){
 66         var arg = name + "=";
 67         var alen = arg.length;
 68         var clen = document.cookie.length;
 69         var i = 0;
 70         while (i < clen)
 71         {
 72             var j = i + alen;
 73             if (document.cookie.substring(i, j) == arg)
 74             return this.getCookieVal(j);
 75             i = document.cookie.indexOf(" ", i) + 1;
 76             if (i == 0) break;
 77         }
 78         return null;
 79   },
 80  
 81  //设置cookie
 82   setCookie: function(sName, sValue,days,sDomain){ 
 83       var expires = new Date();  
 84       expires.setTime(expires.getTime() + parseInt(days)*24*60*60*1000);
 85       document.cookie = sName + "=" + escape(sValue) + ";expires="+expires.toGMTString()+" ;path=/; domain=" + sDomain;      
 86  },
 87  //字符串转码-提交给后端的数据
 88  enCodeXML: function(text){
 89     return text.toString().replace(/&/g, "&amp;")
 90         .replace(/</g, "&lt;")
 91         .replace(/>/g, "&gt;")
 92         .replace(/"/g, "&quot;")
 93         .replace(/'/g, "&apos;");
 94 
 95  },
 96  deCodeXML: function(text){
 97  },
 98  //页面展示数据
 99  htmlEncode: function(str){
100      if (typeof str == "undefined") return "";
101         str = str.replace(/&/g, "&amp;");
102         str = str.replace(/</g, "&lt;");
103         str = str.replace(/>/g, "&gt;");
104         str = str.replace(/\"/g, "&quot;");
105         //str = str.replace(/\'/g, "&apos;"); //IE不支持apos
106         str = str.replace(/ /g, "&nbsp;");
107         str = str.replace(/&amp;#([^\;]+);/ig, "&#$1;"); //将&#20117;转成相应的汉字“井”
108         return str;
109 
110  },
111  htmlDecode:function (str){
112         if (typeof str == "undefined") return "";
113         str = str.replace(/&lt;/g, "<");
114         str = str.replace(/&gt;/g, ">");
115         str = str.replace(/&quot;/g, "\"");
116         str = str.replace(/&apos;/g, "'");
117         str = str.replace(/&nbsp;/g, " ");
118         str = str.replace(/&amp;/g, "&");
119         return str;
120     }
121     
122 };
123 
124 function Ajax(url, args) {
125     this.url = url || "";
126     this.params = args.parameters || "";
127     this.mime = args.mime || "text/html";
128     this.onComplete = args.onComplete || this.defaultOnCompleteFunc;
129     this.onLoading= args.onLoading || this.defaultOnLoadingFunc;
130     this.onError = args.onError || this.defaultOnErrorFunc;
131     this.method = args.method || "post";
132     if (typeof(args.sync) == "undefined" || args.sync == null) { 
133         this.sync = true;
134     } else {
135         this.sync = args.sync ? true : false; 
136     }
137 }
138 
139 Ajax.prototype = {
140     READY_STATE_COMPLETE : 4,
141     getRequest : function () {
142         var funcs = [
143             function() {return new ActiveXObject('Msxml2.XMLHTTP')},
144             function() {return new ActiveXObject('Microsoft.XMLHTTP')},
145             function() {return new XMLHttpRequest()}
146         ];
147 
148         var req = null;
149         for (var i = 0; i < funcs.length; i++) {
150             var f = funcs[i];
151             try {
152             req = f();
153             break;
154             } catch (e) {}
155         }
156 
157         return req || false;
158     },
159 
160     parseParams : function () {
161         if (typeof (this.params) == "string") {
162             return this.params;
163         } else {
164             var s = "";
165             for (var k in this.params) {
166             s += k + "=" + this.params[k] + "&";
167             }
168             return s;
169         }
170     },
171 
172     loadData : function () {
173         this.req = this.getRequest();
174         
175         if (this.req) {
176             this.onLoading();
177             try {
178             var loader = this;
179             this.req.onreadystatechange = function () {
180                 if (loader.req.readyState == loader.READY_STATE_COMPLETE) {
181                 loader.onComplete.call(loader, loader.req);
182                 }
183             }
184             this.req.open(this.method, this.url, this.sync);
185 
186             if (this.method == "post") {
187                 this.req.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
188             }
189 
190             if (this.req.overrideMimeType) {
191                 this.req.overrideMimeType(this.mime);
192             }
193             this.req.send(this.method == "post" ? this.parseParams(this.params) : null);
194             } catch (e) {        
195             this.onError.call(this, e);
196             }
197         }
198     },
199 
200     defaultOnCompleteFunc : function () {
201         alert(this.req.responseText);
202     },
203 
204     defaultOnLoadingFunc : function () {
205     },
206 
207     defaultOnErrorFunc : function (error) {
208     }
209 

后续在慢慢补充..

1 事件监听
2 function addListener(element,e,fn){
3     element.addEventListener ? element.addEventListener(e,fn,false) : element.attachEvent("on" + e,fn);
4 }

 

 

posted on 2012-12-13 14:33  tianqing52  阅读(2113)  评论(0编辑  收藏  举报