封装框架js

   1 (function(win) {
   2     //string展
   3     String.prototype.trim = function() {
   4         return this.replace(/(^\s*)|(\s*$)/g, "");
   5     }
   6     String.prototype.LTrim = function() {
   7         return this.replace(/(^\s*)/g, "");
   8     }
   9     String.prototype.RTrim = function() {
  10         return this.replace(/(\s*$)/g, "");
  11     }
  12     String.prototype.isEmail = function() {
  13         return /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/.test(this);
  14     }
  15     String.prototype.existChinese = function() {
  16         return /^[\x00-\xff]*$/.test(this);
  17     }
  18     String.prototype.isUrl = function() {
  19         return new RegExp("^http[s]?:\/\/([\w-]+\.)+[\w-]+([\w-./?%&=]*)?$", "i").test(this);
  20         //return /^http[s]?:\/\/([\w-]+\.)+[\w-]+([\w-./?%&=]*)?$/i.test(this);
  21     }
  22     String.prototype.isPhoneCall = function() {
  23         return /(^[0-9]{3,4}\-[0-9]{3,8}$)|(^[0-9]{3,8}$)|(^\([0-9]{3,4}\)[0-9]{3,8}$)|(^0{0,1}13[0-9]{9}$)/.test(this);
  24     }
  25     String.prototype.isNumber = function() {
  26         return /^[0-9]+$/.test(this);
  27     }
  28     String.prototype.startWith = function(str) {
  29         return this.indexof(str) == 0;
  30     }
  31 
  32     Array.prototype.insert = function(startIndex, data) {
  33         this.splice(startIndex, 0, data);
  34         return this;
  35     }
  36     Array.prototype.del = function(startIndex, delLen) {
  37         this.splice(startIndex, delLen);
  38         return this;
  39     }
  40     Array.prototype.replace = function(index, obj) {
  41         this.splice(index, 1, obj);
  42         return this;
  43     }
  44     Array.prototype.subArray = function(startIndex, len) {
  45         return this.slice(startIndex, startIndex + len);
  46     }
  47 
  48     Date.prototype.getDateString = function() {  //鏄剧ず 骞存湀鏃?
  49         return this.toLocaleDateString();
  50     }
  51     Date.prototype.getTimeString = function() {  //鏄剧ず 鏃跺垎绉?
  52         return this.toLocaleTimeString();
  53     }
  54 
  55     //瀵筬irefox winodw.event鐨勬墿灞?
  56     function __firefox() {
  57         HTMLElement.prototype.__defineGetter__("runtimeStyle", __element_style);
  58         window.constructor.prototype.__defineGetter__("event", __window_event);
  59         Event.prototype.__defineGetter__("srcElement", __event_srcElement);
  60     }
  61     function __element_style() {
  62         return this.style;
  63     }
  64     function __window_event() {
  65         return __window_event_constructor();
  66     }
  67     function __event_srcElement() {
  68         return this.target;
  69     }
  70     function __window_event_constructor() {
  71         if (document.all) {
  72             return window.event;
  73         }
  74         var _caller = __window_event_constructor.caller;
  75         while (_caller != null) {
  76             var _argument = _caller.arguments[0];
  77             if (_argument) {
  78                 var _temp = _argument.constructor;
  79                 if (_temp.toString().indexOf("Event") != -1) {
  80                     return _argument;
  81                 }
  82             }
  83             _caller = _caller.caller;
  84         }
  85         return null;
  86     }
  87     if (window.addEventListener) {
  88         __firefox();
  89     }
  90 
  91     var isIE = (navigator.userAgent.toUpperCase().indexOf("MSIE")) > 0 ? true : false;
  92     var isFireFox = (navigator.userAgent.toUpperCase().indexOf("FIREFOX")) > 0 ? true : false;
  93     var isChrome = (navigator.userAgent.toUpperCase().indexOf("CHROME")) > 0 ? true : false;
  94     var httpRequest = function() {
  95         var request = null;
  96         if (window.XMLHttpRequest) {
  97             request = new XMLHttpRequest();
  98         }
  99 
 100         if (window.ActiveXObject) {
 101             try {
 102                 request = new ActiveXObject("Msxml2.XMLHTTP");
 103             }
 104             catch (e) {
 105                 try {
 106                     request = new ActiveXObject("Microsoft.XMLHTTP");
 107                 }
 108                 catch (e) {
 109                 }
 110             }
 111         }
 112         return request;
 113     };
 114 
 115     var _$ = function(selector) {
 116         //Array展
 117         //if(/Array/.test((typeof selector=='string')?selector:(selector.consturctor?selector.constructor.toString():""))){
 118         if (selector.constructor && !/object HTMLCollection/.test(selector.constructor.toString()) && (/object array/.test(Object.prototype.toString.call(selector).toLowerCase()) || /object htmlcollection/.test(Object.prototype.toString.call(selector).toLowerCase()))) {
 119             selector.each = function(fn) {
 120                 for (var k = 0; k < selector.length; k++) {
 121                     if (k != 'length') {
 122                         if (selector.toString().indexOf("HTML") > 0) {
 123 
 124                         }
 125                         fn.call(this[k], k, this[k]);
 126                     }
 127                 }
 128             }
 129             //展诖
 130 
 131             return selector;
 132         }
 133 
 134         //远展
 135         //if(/Object/.test((typeof selector=='string')?selector:(selector.consturctor?selector.constructor.toString():""))){
 136         if (selector.constructor && !/object NodeList/.test(selector.constructor.toString()) && !/object HTMLCollection/.test(selector.constructor.toString()) && selector != undefined && /object object/.test(Object.prototype.toString.call(selector).toLowerCase()) && (typeof selector != 'string') ? selector.innerHTML == undefined : false) {
 137             selector.each = function(fn) {
 138                 for (var key in selector) {
 139                     if (key == 'length' || key == 'each') continue;
 140                     //if((typeof selector[key]) !='function')
 141                     fn.call(selector[key], key, selector[key]);
 142                 }
 143             }
 144             //展诖
 145 
 146             return selector;
 147         }
 148 
 149         var ele = {};
 150         ele.find = function(selector) {
 151             if (!selector) {
 152                 return null;
 153             }
 154 
 155             
 156             function outhtml(selector) {
 157                 var _DIV = document_createElement_x_x('div');
 158                 if (typeof selector != 'string') {
 159                     _DIV.a(selector.cloneNode(false));
 160                     return _DIV.innerHTML;
 161                 }
 162 
 163                 return "";
 164             }
 165 
 166             if (/^#/.test(selector)) {
 167                 return _$.prototype.id(selector);
 168             }
 169             else if (/^\./.test(selector)) {
 170                 return _$.prototype.cls(selector);
 171             } else if ((typeof selector != 'string') && (!(/object array/.test(selector)) || new RegExp("(S*?)[^>]*>.*?|<.*?/>").test(selector.outerHTML ? selector.outerHTML : outhtml(selector)))) {//}else if(new RegExp("(S*?)[^>]*>.*?|<.*?/>").test(selector.outerHTML?selector.outerHTML:outhtml(selector))){
 172                 return _$.prototype.htmlEle(selector);
 173             }
 174             else {
 175                 //alert(selector);
 176                 return _$.prototype.ele(selector);
 177             }
 178         }
 179 
 180         ele.col = ele.find(selector);
 181 
 182         ele.col.each = function(fn) {         //一趴
 183             //樱支莼
 184             if (this.length == undefined) {
 185                 if (typeof fn == 'function') {
 186                     fn.call(this, 0, this);
 187                 } else {
 188                     this.data = function(key, value) {
 189                         this.cache = this.cache || {};
 190                         if (key && value) {
 191                             this.cache[key] = value;
 192                             //eval_r('(this.cache.'+key+'="'+value+'")');
 193                         } else {
 194                             //return eval_r('this.cache.'+key);
 195                             return this.cache[key];
 196                         }
 197                     }
 198                 }
 199             }
 200 
 201             for (var i = 0; i < this.length; i++) {                //each氐
 202                 if (typeof fn == 'function') {
 203                     fn.call(this[i], i, this[i]);
 204                 } else {                            //data莼
 205                     this[i].data = function(key, value) {
 206                         this.cache = this.cache || {};
 207                         if (key && value) {
 208                             //eval_r('(this.cache.'+key+'="'+value+'")');
 209                             this.cache[key] = value;
 210                         } else {
 211                             //return eval_r('this.cache.'+key);
 212                             return this.cache[key];
 213                         }
 214                     }
 215                 }
 216             }
 217         };
 218 
 219         //支旨虾头羌
 220         ele.col.bind = function(typ, fn) {
 221             var evr = function() { fn.call(this, window.event); }; //屑没
 222 
 223             if (this.length) {
 224                 for (var i = 0; i < this.length; i++) {
 225                     if (typeof fn == 'function') {
 226                         eval_r((isIE ? "this[i].on" : (isFireFox ? "this[i]." : "noneEvent.")) + typ + "=" + evr);
 227                         if (isFireFox) {//
 228                             this[i].addEventListener(typ, evr, false);
 229                         }
 230                     }
 231                     if (isChrome) {
 232                         eval_r("this.on" + typ + "=" + evr);
 233                     }
 234                 }
 235             } else {//id取玫
 236                 eval_r((isIE ? "this.on" : (isFireFox ? "this." : "noneEvent")) + typ + "=" + evr);
 237                 if (isFireFox) {//
 238                     this.addEventListener(typ, evr, false);
 239                 }
 240                 if (isChrome) {
 241                     eval_r("this.on" + typ + "=" + evr);
 242                 }
 243             }
 244         };
 245 
 246         //ele.col羌希堑元兀取elementhtml
 247         ele.col.html = function(htm) {
 248             if (htm == '' || htm) {
 249                 this.innerHTML = htm;
 250             } else {
 251                 return this.innerHTML;
 252             }
 253         };
 254 
 255         //ele.col羌希堑元
 256         ele.col.css = function(sty, val) {
 257             if (typeof sty == 'object') {
 258                 for (var key in sty) {
 259                     this.style[key] = sty[key];
 260                 }
 261             } else if (typeof sty == 'string' && typeof val == 'string') {
 262                 try {
 263                     this.style[sty] = val;
 264                 }
 265                 catch (e) {
 266                 }
 267             } else {
 268                 return this.style[sty];
 269             }
 270         };
 271 
 272         ele.col.append = function(elem) {
 273             this.a(elem);
 274         };
 275 
 276         ele.col.prepend = function(elem) {
 277             this.insertBefore(elem, $(this).first());
 278         }
 279 
 280         ele.col.after = function(elem) {
 281             var tem = $.html("");
 282             $(this).parent().append(tem);
 283             $(this).parent().insertBefore(elem, tem);
 284             document.body.a(tem);
 285         }
 286 
 287         ele.col.before = function(elem) {
 288             $(this).parent().insertBefore(elem, this);
 289         }
 290 
 291         ele.col.hasClass = function(className) {
 292             var state = false;
 293             var arr = this.className.split(' ');
 294             $(arr).each(function(k, v) {
 295                 if (v && v.toString() == className) {
 296                     state = true;
 297                 }
 298             });
 299 
 300             return state;
 301         }
 302 
 303         ele.col.addClass = function(className) {
 304             var arr = this.className.split(' ');
 305             var state = false;
 306             $(arr).each(function(k, v) {
 307                 if (v && v.toString() == className) {
 308                     state = true;
 309                 }
 310             });
 311             if (state) return;
 312 
 313             this.className = this.className.trim();
 314             this.className += " " + className;
 315         }
 316 
 317         ele.col.removeClass = function(className) {
 318             var arr = this.className.split(' ');
 319             $(arr).each(function(k, v) {
 320                 if (v && v.toString() == className) {
 321                     arr[k] = "";
 322                 }
 323             });
 324 
 325             this.className = arr.join(' ').trim(); //this.className.toLowerCase().replace(className,"");
 326         }
 327 
 328         ele.col.replaceClass = function(newName, oldName) {
 329             this.removeClass(oldName);
 330             this.addClass(newName);
 331         }
 332 
 333         //绾ц仈鏌ユ壘鍏冪礌
 334         ele.col.find = function(tagName) {
 335             if (tagName.indexOf("#") >= 0)
 336                 return $(tagName);
 337             if ("div,span,a,input,table,tr,td,button".indexOf(tagName) >= 0)
 338                 return $(this.getElementsByTagName_r(tagName));
 339 
 340             var eleCollection = [];
 341             var nodeCol = this.getElementsByTagName_r("*");
 342             var allEle = $(tagName);
 343             $(allEle).each(function(key, val) {
 344                 $(nodeCol).each(function(k, v) {
 345                     if (val == v)
 346                         eleCollection.push(v);
 347                 });
 348             });
 349 
 350             return $(eleCollection);
 351         }
 352 
 353         ele.col.toggle = function(fn1, fn2) {
 354             this.bind('click', function() {
 355                 if ($(this).data('state') == undefined) {
 356                     $(this).data('state', "false");
 357                 }
 358 
 359                 if (typeof fn1 == 'function' && typeof fn2 == 'function') {
 360                     if ($(this).data('state') == 'true') {
 361                         $(this).data('state', 'false');
 362                         fn2.call(this, fn2.arguments);
 363                     } else {
 364                         $(this).data('state', 'true');
 365                         fn1.call(this, fn1.arguments);
 366                     }
 367                 }
 368             });
 369         }
 370 
 371         ele.col.removeData = function(attrName) {
 372             if ($(this).cache) {
 373                 return delete $(this).cache[attrName];
 374             }
 375             return false;
 376         };
 377 
 378         ele.col.attr = function(k, v) {
 379             if (k && v)
 380                 this.setAttribute(k, v);
 381             else
 382                 return this.getAttribute(k);
 383         }
 384 
 385         ele.col.removeAttr = function(k) {
 386             return this.removeAttribute(k);
 387         }
 388 
 389         ele.col.val = function(str) {
 390             if (!this.tagName) {              //鏄竴涓猦tmlCollection
 391                 if (str) {
 392                     this.each(function(k, v) {
 393                         $(str).each(function(key, val) {
 394                             if (v.value == val) {
 395                                 v.checked = true;
 396                             }
 397                         });
 398                     });
 399                 } else {//杩斿洖閫変腑鐨勮妭鐐?
 400                     var selectedCollection = [];
 401                     this.each(function(k, v) {
 402                         if (v.checked)
 403                             selectedCollection.push(v);
 404                     });
 405                     return $(selectedCollection);
 406                 }
 407 
 408                 return;
 409             }
 410 
 411             switch (this.tagName.toLowerCase()) {//璁剧疆鍗曚釜鍏冪礌
 412                 case 'input':
 413                     switch ($(this).attr('type')) {
 414                         case 'button':
 415                         case 'text':
 416                         case 'area':
 417                             if (str) {
 418                                 $(this).attr('value', str);
 419                             } else {
 420                                 return this.value;
 421                             }
 422                             break;
 423                         case 'radio':
 424                         case 'checkbox':
 425                             if (str) {
 426                                 $(this).attr('checked', str);
 427                             } else {
 428                                 return this.checked ? true : false;
 429                             }
 430                             break;
 431                         default:
 432                             break;
 433                     }
 434                     break;
 435                 case 'select':
 436                     var me = this;
 437                     if (str) {
 438                         $(this.options).each(function(k, v) {
 439                             if (me[k].value.toString() == str.toString()) {
 440                                 me.selectedIndex = k;
 441                             }
 442                         });
 443                     } else {
 444                         return this.options[this.selectedIndex].value;
 445                     }
 446                     break;
 447                 case 'textarea':
 448                     if (str) {
 449                         $(this).attr('value', str);
 450                     } else {
 451                         return this.value;
 452                     }
 453                     break;
 454                 default:
 455                     break;
 456             }
 457         }
 458 
 459         ele.col.hasChild = function() {
 460             return this.hasChildNodes();
 461         }
 462 
 463         ele.col.prev = function() {
 464             var fNode = null;
 465             var findNodes = function(node) {
 466                 if (node.previousSibling) {
 467                     if (node.previousSibling.nodeType != 1) {
 468                         findNodes(node.previousSibling);
 469                     } else {
 470                         fNode = node.previousSibling;
 471                     }
 472                 } else {
 473                     return null;
 474                 }
 475             }
 476 
 477             findNodes(this);
 478             return $(fNode);
 479         }
 480 
 481         ele.col.next = function() {
 482             var fNode = null;
 483             var findNodes = function(node) {
 484                 if (node.nextSibling) {
 485                     if (node.nextSibling.nodeType != 1) {
 486                         findNodes(node.nextSibling);
 487                     } else {
 488                         fNode = node.nextSibling;
 489                     }
 490                 } else {
 491                     return null;
 492                 }
 493             }
 494 
 495             findNodes(this);
 496             return $(fNode);
 497         }
 498 
 499         ele.col.first = function() {
 500             var fNode = null;
 501             var findNodes = function(node) {
 502                 if (node && node.nodeType != 1) {
 503                     findNodes(node.nextSibling);
 504                 }
 505                 else {
 506                     fNode = node;
 507                 }
 508             }
 509 
 510             findNodes(this.firstChild);
 511             return $(fNode);
 512         }
 513 
 514         ele.col.last = function() {
 515             var fNode = null;
 516             var findNodes = function(node) {
 517                 if (node && node.nodeType != 1) {
 518                     findNodes(node.previousSibling);
 519                 }
 520                 else {
 521                     fNode = node;
 522                 }
 523             }
 524 
 525             findNodes(this.lastChild);
 526             return $(fNode);
 527         }
 528 
 529         ele.col.parent = function() {
 530             return $(this.parentNode);
 531         }
 532 
 533         ele.col.siblings = function() {
 534             var fNode = $(this).parent().child();
 535             var me = this;
 536             $(fNode).each(function(k, v) {
 537                 if (v == me) {
 538                     fNode.del(k, 1);
 539                 }
 540             });
 541 
 542             return $(fNode);
 543         }
 544 
 545         ele.col.child = function() {
 546             var fNode = [];
 547             for (var i = 0; i < this.childNodes.length; i++) {
 548                 if (this.childNodes[i].nodeType == 1) {
 549                     fNode.push(this.childNodes[i]);
 550                 }
 551             }
 552 
 553             return $(fNode);
 554         }
 555 
 556         ele.col.remove = function() {
 557             $(this).each(function(k, v) {
 558                 $(v).parent().removeChild(v);
 559             });
 560         }
 561 
 562         ele.col.nextAll = function() {
 563             var fNode = [];
 564             var findNodes = function(node) {
 565                 if (node && node.nodeType != 1) {
 566                     findNodes(node.nextSibling);
 567                 }
 568                 else {
 569                     if (node) {
 570                         findNodes(node.nextSibling);
 571                         fNode.push(node);
 572                     }
 573                 }
 574             }
 575 
 576             findNodes($(this).next());
 577             return $(fNode);
 578         }
 579 
 580         ele.col.prevAll = function() {
 581             var fNode = [];
 582             var findNodes = function(node) {
 583                 if (node && node.nodeType != 1) {
 584                     findNodes(node.previousSibling);
 585                 }
 586                 else {
 587                     if (node) {
 588                         findNodes(node.previousSibling);
 589                         fNode.push(node);
 590                     }
 591                 }
 592             }
 593 
 594             findNodes($(this).prev());
 595             return $(fNode);
 596         }
 597 
 598         ele.col.slideUp = function() {
 599             if (this.css('height') == '0px') {   //瑙e喅ie杩炵画闅愯棌鎶ラ敊闂
 600                 return;
 601             }
 602             var obj = {
 603                 ele: this,
 604                 startHeight: this.offsetHeight == 0 ? parseInt(this.style.height.substring(0, this.style.height.indexOf('px'))) : this.offsetHeight,
 605                 i: 1,
 606                 endHeight: 1
 607             };
 608 
 609             this.data('slideUpHeight', obj.startHeight);
 610             obj.ele.css("height", obj.startHeight + 'px');
 611             obj.ele.css('overflow', 'hidden');
 612 
 613             $.animateQueue.insert($.animateQueue.length, { state: false, ele: this });
 614             $.wait(this, function() {
 615                 var id = window.setInterval(function() {
 616                     (function(o) {
 617                         o.startHeight -= obj.i * (isIE ? 1 : 0.06);
 618                         obj.i++;
 619 
 620                         if (o.startHeight > 10)
 621                             obj.ele.css("height", o.startHeight + "px");
 622                         else {
 623                             obj.endheight = obj.endHeight * (isIE ? 2 : 0.12);
 624                             var h = obj.ele.css("height");
 625                             obj.ele.css("height", (parseInt(h.substring(0, h.indexOf('px'))) - obj.endHeight) + 'px');
 626                             if (parseInt(obj.ele.css('height').substring(0, 2)) <= 0) {
 627                                 obj.ele.css("height", '0px');
 628                                 obj.ele.css('display', 'none');
 629                                 window.clearInterval(id);
 630                             }
 631                         }
 632                     })(obj);
 633                 }, 1);
 634                 if ($.animateQueue.length > 1) {
 635                     $.animateQueue[1].state = true;
 636                 }
 637                 $.animateQueue.del(0, 1);
 638             });
 639         }
 640 
 641         ele.col.slideDown = function() {
 642             this.css('display', 'block');
 643             this.css('overflow', 'hidden');
 644 
 645             var obj = { ele: this, startHeight: 1, i: 1, endHeight: (this.data('slideUpHeight') ? this.data('slideUpHeight') : parseInt(this.style.height.substring(0, this.style.height.indexOf('px')))) };
 646             if (obj.endHeight <= 0) {
 647                 obj.endHeight = this.offsetHeight;
 648                 alert(obj.endHeight);
 649             }
 650             obj.ele.css("height", '0px');
 651             obj.ele.css('overflow', 'hidden');
 652             obj.ele.css('display', 'block');
 653 
 654             $.animateQueue.insert($.animateQueue.length, { state: false, ele: this });
 655             $.wait(this, function() {
 656                 var id = window.setInterval(function() {
 657                     (function(o) {
 658                         if (o.startHeight < obj.endHeight - 10) {
 659                             o.startHeight += obj.i * (isIE ? 1 : 0.06);
 660                             obj.i++;
 661                             obj.ele.css("height", o.startHeight + "px");
 662                         } else {
 663                             var ii = o.startHeight++;
 664                             obj.ele.css("height", ii + 'px');
 665                             if (parseInt(obj.ele.css('height').substring(0, obj.ele.css('height').indexOf('px'))) > obj.endHeight) {
 666                                 obj.ele.css('height', obj.endHeight + "px");
 667                                 window.clearInterval(id);
 668                             }
 669                         }
 670                     })(obj);
 671                 }, 1);
 672 
 673                 if ($.animateQueue.length > 1) {
 674                     $.animateQueue[1].state = true;
 675                 }
 676                 $.animateQueue.del(0, 1);
 677             });
 678         }
 679 
 680         ele.col.empty = function() {
 681             this.innerHTML = "";
 682         }
 683 
 684         
 685 
 686         ele.col.paddingLeft = function() {
 687             if (isIE) {
 688                 return window.event.offsetX;
 689             } else if (isFireFox) {
 690                 return $.window.x() - this.left();
 691             } else if (isChrome) {
 692                 return window.event.offsetX;
 693             }
 694         };
 695 
 696         ele.col.paddingTop = function() {
 697             if (isIE) {
 698                 return window.event.offsetY;
 699             } else if (isFireFox) {
 700                 return $.window.y() - this.top();
 701             } else if (isChrome) {
 702                 return window.event.offsetY;
 703             }
 704         };
 705 
 706         ele.col.left = function() {
 707             if (isIE) {
 708                 return $.window.x() - window.event.offsetX;
 709             } else if (isFireFox) {
 710                 return $.window.x() - window.event.layerX + $.window.target().offsetLeft;
 711             } else if (isChrome) {
 712                 return $.window.x() - window.event.offsetX;
 713             }
 714         }
 715 
 716         ele.col.top = function() {
 717             if (isIE) {
 718                 return $.window.y() - window.event.offsetY;
 719             } else if (isFireFox) {
 720                 return $.window.y() - window.event.layerY + $.window.target().offsetTop;
 721             } else if (isChrome) {
 722                 return $.window.y() - window.event.offsetY;
 723             }
 724         }
 725 
 726         ele.col.clone = function(state) {
 727             return this.cloneNode(state);
 728         }
 729 
 730         //蠖ɑ
 731         ele.col.each();
 732 
 733         return ele.col;
 734     };
 735 
 736     //id选
 737     _$.prototype.id = function(selector) {
 738         if (selector.indexOf('#') > -1) {
 739             return document.getElementByIdx_x_x(selector.trim().replace(/#/, ''));
 740         }
 741     }
 742 
 743     //class选
 744     _$.prototype.cls = function(selector) {
 745         if (selector.indexOf('.') > -1) {
 746             var eleCollection = [];
 747             var allEle = document.body.getElementsByTagName_r('*');
 748             for (var i = 0; i < allEle.length; i++) {
 749                 if (allEle[i].className && allEle[i].className.indexOf(selector.trim().replace(/^./, '')) > -1) {
 750                     eleCollection.push(allEle[i]);
 751                 }
 752             }
 753             return eleCollection;
 754         }
 755 
 756         return [];
 757     }
 758 
 759     //元选
 760     _$.prototype.ele = function(selector) {
 761         var eleCollection = [];
 762         var allEle = document.body.getElementsByTagName_r('*');
 763         for (var i = 0; i < allEle.length; i++) {
 764             if (allEle[i].tagName.toString().toUpperCase() == selector.toString().toUpperCase()) {
 765                 eleCollection.push(allEle[i]);
 766             }
 767         }
 768         return eleCollection;
 769     }
 770 
 771     _$.prototype.htmlEle = function(selector) {
 772         return selector;
 773     }
 774 
 775     _$.html = function(htm) {
 776         if (htm.toLowerCase().indexOf("<") != 0) {
 777             return document_createTextNode(htm);
 778         }
 779 
 780         if (htm.toLowerCase().indexOf('thead') == 1) {
 781             return document_createElement_x_x('thead');
 782         };
 783         if (htm.toLowerCase().indexOf('tbody') == 1) {
 784             return document_createElement_x_x('tbody');
 785         };
 786         if (htm.toLowerCase().indexOf('tfoot') == 1) {
 787             return document_createElement_x_x('tfoot');
 788         };
 789         if (htm.toLowerCase().indexOf('th') == 1) {
 790             var text = htm.toLowerCase().substring(4, htm.length - 5);
 791             var th = document_createElement_x_x('th');
 792             th.a(document_createTextNode(text));
 793             return th;
 794         };
 795         if (htm.toLowerCase().indexOf('td') == 1) {
 796             var text = htm.toLowerCase().substring(4, htm.length - 5);
 797             var td = document_createElement_x_x('td');
 798             td.a(document_createTextNode(text));
 799             return td;
 800         };
 801         if (htm.toLowerCase().indexOf('tr') == 1) {
 802             return document_createElement_x_x('tr');
 803         };
 804 
 805         var _div = document_createElement_x_x('div');
 806         if (typeof htm == 'string') {
 807             _div.innerHTML = htm;
 808             return _div.firstChild;
 809         }
 810         return null;
 811     }
 812 
 813     _$.ajax = function(obj) {
 814         switch (obj.type.toLowerCase()) {
 815             case "post":
 816                 this.post(obj);
 817                 break;
 818             case "get":
 819                 this.get(obj);
 820                 break;
 821             default:
 822                 break;
 823         }
 824     }
 825 
 826     _$.get = function(obj) {//{url,data,fn beforsend,fn sending,fn success,fn error}
 827         httpRequ = httpRequest();
 828         if (!httpRequ) {
 829             alert("浣犵殑娴忚鍣ㄤ笉鏀寔ajax");
 830             return;
 831         }
 832 
 833         httpRequ.onreadystatechange = function() {
 834             if (httpRequ.readyState == 3) {//鏁版嵁浼犺緭涓?
 835                 if (obj.sending) {
 836                     obj.sending();
 837                 }
 838             }
 839             if (httpRequ.readyState == 4) {
 840                 if (httpRequ.status == 200) {
 841                     var string = httpRequ.responseText;
 842                     if (obj.success) {
 843                         obj.success(string);
 844                     }
 845                 } else {
 846                     var str = "";
 847                     if (httpRequ.status.toString().indexOf('5') == 0) {
 848                         str = '鏈嶅姟鍣ㄥ唴閮ㄩ敊璇?';
 849                     } else if (httpRequ.status.toString().indexOf('4') == 0) {
 850                         str = '瀹㈡埛绔敊璇?';
 851                     }
 852 
 853                     if (obj.error) {
 854                         obj.error(str);
 855                     }
 856                 }
 857             }
 858         }
 859 
 860         httpRequ.open('GET', obj.url, false);
 861         if (obj.beforSend) {
 862             obj.beforSend();
 863         }
 864         httpRequ.send();
 865     }
 866 
 867     _$.post = function(obj) {
 868         httpRequ = httpRequest();
 869         if (!httpRequ) {
 870             alert("浣犵殑娴忚鍣ㄤ笉鏀寔ajax");
 871             return;
 872         }
 873 
 874         httpRequ.onreadystatechange = function() {
 875             if (httpRequ.readyState == 3) {//鏁版嵁浼犺緭涓?
 876                 if (obj.sending) {
 877                     obj.sending();
 878                 }
 879             }
 880             if (httpRequ.readyState == 4) {
 881                 if (httpRequ.status == 200) {
 882                     var string = httpRequ.responseText;
 883                     if (obj.success) {
 884                         obj.success(string);
 885                     }
 886                 } else {
 887                     var str = "";
 888                     if (httpRequ.status.toString().indexOf('5') == 0) {
 889                         str = '鏈嶅姟鍣ㄥ唴閮ㄩ敊璇?';
 890                     } else if (httpRequ.status.toString().indexOf('4') == 0) {
 891                         str = '瀹㈡埛绔敊璇?';
 892                     }
 893 
 894                     if (obj.error) {
 895                         obj.error(str == "" ? "鏈煡閿欒" : str);
 896                     }
 897                 }
 898             }
 899         }
 900 
 901         httpRequ.open('POST', obj.url, false);
 902         if (obj.beforSend) {
 903             obj.beforSend();
 904         }
 905 
 906         httpRequ.send(obj.data);
 907     }
 908 
 909     _$.json = function(obj) {
 910         return eval_r('(' + obj + ')');
 911     }
 912 
 913     _$.jsonToString = function(o) {
 914         if (o == undefined) {
 915             return "";
 916         }
 917         var r = [];
 918         if (typeof o == "string") return "\"" + o.replace(/([\"\\])/g, "\\$1").replace(/(\n)/g, "\\n").replace(/(\r)/g, "\\r").replace(/(\t)/g, "\\t") + "\"";
 919         if (typeof o == "object") {
 920             if (!o.sort) {
 921                 for (var i in o)
 922                     r.push(i + ":" + _$.jsonToString(o[i]));
 923                 if (!!document.all && !/^\n?function\s*toString\(\)\s*\{\n?\s*\[native code\]\n?\s*\}\n?\s*$/.test(o.toString)) {
 924                     r.push("toString:" + o.toString.toString());
 925                 }
 926                 r = "{" + r.join() + "}"
 927             } else {
 928                 for (var i = 0; i < o.length; i++)
 929                     r.push(_$.jsonToString(o[i]))
 930                 r = "[" + r.join() + "]";
 931             }
 932             return r;
 933         }
 934         return o.toString().replace(/\"\:/g, '":""');
 935     }
 936 
 937     _$.stopEvent = function(e) {
 938         if (event.stopPropagation) event.stopPropagation();
 939         else event.cancelBubble = true;
 940     }
 941 
 942     _$.intervalSend = function(interval, obj, fn) {
 943         return window.setInterval(function() { fn(obj); }, interval, obj);
 944     }
 945 
 946     _$.ready = function(fn) {
 947         this.state = false;
 948         var id = window.setInterval(function() {
 949             (function() {
 950                 if (document.body) {
 951                     window.clearInterval(id);
 952                     if (!this.state) {
 953                         this.state = true;
 954                         fn.call();
 955                     }
 956                 }
 957             })();
 958         }, 1);
 959     }
 960 
 961     //鍔ㄧ敾涓撶敤
 962     _$.animateQueue = [];
 963     _$.wait = function(ele, fn) {
 964         window.setTimeout(function() {
 965             if (_$.animateQueue.length >= 1) {
 966                 _$.animateQueue[0].state = true;
 967             }
 968 
 969             var id = window.setInterval(function() {
 970                 (function() {
 971                     if (_$.animateQueue[0].state && _$.animateQueue[0].ele == ele) {
 972                         window.clearInterval(id);
 973                         fn.call(ele);
 974                     }
 975                 })();
 976             }, 1)
 977         }, 200);
 978 
 979     }
 980 
 981     _$.window = {
 982         x: function() {                        //相对于body左上角的距离
 983             if (isIE) {
 984                 return window.event.x;
 985             } else if (isFireFox) {
 986                 return window.event.pageX;
 987             } else if (isChrome) {
 988                 return window.event.x;
 989             }
 990         },
 991         y: function() {
 992             if (isIE) {
 993                 return window.event.y;
 994             } else if (isFireFox) {
 995                 return window.event.pageY;
 996             } else if (isChrome) {
 997                 return window.event.y;
 998             }
 999         },
1000         clientX: function() {                    //相对于body左上角的距离,不包括边框
1001             return window.event.clientX;
1002         },
1003         clientY: function() {
1004             return window.event.clientY;
1005         },
1006         width: function() {
1007             return window.event.clientWidth;
1008         },
1009         height: function() {
1010             if (window.event) {
1011                 return window.event.clientHeight;
1012             } else {
1013                 return $(document.body).height();
1014             }
1015         },
1016         fromElement: function() {
1017             if (isIE) {
1018                 return window.event.fromElement;
1019             } else if (isFireFox) {
1020                 return window.event.relatedTarget;
1021             }
1022         },
1023         toElement: function() {
1024             if (isIE) {
1025                 return window.event.srcElement;
1026             } else if (isFireFox) {
1027                 return window.event.target;
1028             }
1029         },
1030         target: function() {
1031             if (isIE) {
1032                 return window.event.srcElement;
1033             } else if (isFireFox) {
1034                 return window.event.target;
1035             }
1036         }
1037     }
1038 
1039     //展涌
1040     _$.extend = function(fnName, fn) {
1041         eval_r("_$." + fnName + "=" + fn);
1042     };
1043 
1044     win.$ = _$;
1045 })(window);
View Code

 

posted @ 2016-07-01 15:59  灬東歌℡  阅读(170)  评论(0)    收藏  举报