DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> JavaScript入門知識 >> JavaScript基礎知識 >> javascript工具庫代碼
javascript工具庫代碼
編輯:JavaScript基礎知識     
復制代碼 代碼如下:
/*
 *YYJ.js 主要提供後台應用方法
 *目前有Ajax、addLoadEvent、deleteAll、getByClass、$C(=getByClass)、$、yyjtable、setCss、getCss、iaArray等方法。最後面還提供了一個
 *可實例化的方法YYJ.tick用來計算腳本運行時間
 *版本0.1    --tianyi    yyj
 */
 var YYJ=function(){
     var UniqueInstance;
     function constrotor(){
         return {
             //ajax方法
             Ajax:function(method,url,data,success,fail){
                 var isget=method.toLowerCase()=="get";
                 var xmlhttp=window.XMLHttpRequest?new XMLHttpRequest():new ActiveXObject("Microsoft.XMLHTTP");
                 xmlhttp.open(method,url,true);
                 if(!isget)xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
                 xmlhttp.onreadystatechange=function(){
                         if(xmlhttp.readyState==4){
                             if(xmlhttp.status==200){
                                 if(success)success(xmlhttp);
                             }else{
                                 if(fail)fail(xmlhttp);
                             }
                         }
                 };
                 xmlhttp.send(isget?null:data);
             },//Ajax
             addLoadEvent:function(func) {
                 var oldonload = window.onload;
                 if (typeof window.onload != 'function') {
                     window.onload = func;
                 } else {
                     window.onload = function() {
                       if (oldonload) {
                         oldonload();
                       }
                       func();
                     }
                 }
             },//addLoadEvent
             deleteAll:function(checkallbtn,checksomebtn,submitbtn){
                 var checkall=YYJ.$(checkallbtn);
                 var checksome=YYJ.$(checksomebtn);
                 var submit1=YYJ.$(submitbtn);
                 var checkboxs=(function(){
                     var arr=[];
                     var check=document.getElementsByTagName("input");
                     for(i=0;i<check.length;i++){
                         if(check[i].getAttribute("type")!="checkbox")
                             continue;
                         arr.push(check[i]);
                     }
                     return arr;
                 })();
                 checkall.onclick=function(){
                     checkall.clicked=true;
                     for(var i=0;i<checkboxs.length;i++){
                         if(!checkboxs[i].checked){
                             checkall.clicked=false;
                             break;
                         }
                     }
                     if(!checkall.clicked){
                         for(var i=0;i<checkboxs.length;i++){
                             checkboxs[i].checked=true;
                         }
                     }else{
                         for(var i=0;i<checkboxs.length;i++){
                             checkboxs[i].checked=false;
                         }
                     }
                 }
                 checksome.onclick=function(){
                     for(var x in checkboxs){
                         checkboxs[x].checked=!checkboxs[x].checked;
                     }
                 }
                 submit1.onclick=function(){
                     var haschecked=false;
                     var beSureDel=false;
                     for(var i=0;i<checkboxs.length;i++){
                         if(checkboxs[i].checked){
                             haschecked=true;
                             break;
                         }
                     }
                     haschecked?function(){
                         beSureDel=confirm("確定要刪除選中項嗎?");
                     }():function(){
                         alert("沒有選中項!");
                         beSureDel=false;
                     }();
                     return beSureDel;
                 }
             },//deleteall
             getByClass:function(classname,parent,nodename){
                 var s=(parent||document).getElementsByTagName(nodename||"*");
                 return function(){
                     var a=[];
                     for(var i=0,j=s.length;i<j;i++){
                         if(!s[i].className) continue;
                         var name=" "+s[i].className+" ";
                         if(name.indexOf(" "+classname+" ")!=-1){
                             a.push(s[i]);
                         }
                     }
                     return a;
                 }();
             },
             $C:function(classname,parent,nodename){
                 return YYJ.getByClass(classname,parent,nodename);
             },//getByClass
             $:function(str){
                 return document.getElementById(str);
             },//getById
             yyjtable:function(tableId){
                 var tbl=YYJ.$(tableId);
                 var trs=tbl.getElementsByTagName("tr");
                 for(var i=1;i<trs.length;i++){
                     if(i%2!=0){
                         trs[i].style.backgroundColor="#FFFFF0";
                     }else{
                         trs[i].style.backgroundColor="white";
                     }
                     trs[i].onmouseover=function(){
                         this.col1=this.style.backgroundColor;
                         this.style.backgroundColor="#FFFACD";
                     }
                     trs[i].onmouseout=function(){
                         this.style.backgroundColor=this.col1;
                     }
                 }
             },//yyjtable
             /*使用方法
             YYJ.setCss([YYJ.$("table1")],{
                 color:"red",
                 backgroundColor:"silver"
             });*/
             setCss:function(eles,opt){
                 if(!eles||!opt)return;
                 if(!eles.length){
                     throw new Error("setCss的第一個參數要求為數組!");
                 }
                 for(var i=0;j=eles[i];i++){
                     try{
                         for(var x in opt){
                             j.style[x]=opt[x];
                         }
                     }catch(ex){}
                 }
             },//setCss
             /*使用方法
             var css=YYJ.getCss(YYJ.$("table1"),["backgroundColor"]);
             alert(css["backgroundColor"]);*/
             getCss:function(ele,opt){
                 if(!this.isArray(opt)){
                     throw new Error("getCss的第二個參數要求為string數組!");
                 }
                 var css={};
                 for(var i=0,j=opt.length;i<j;i++){
                     try{
                         css[opt[i]]=ele.style[opt[i]];
                     }catch(ex){}
                 }
                 return css;

             },//getCss
             isArray:function(opt){
                 return Object.prototype.toString.call(opt)=="[object Array]"
             }

         };
     }
     function getUniqueInstance(){
         if(UniqueInstance){
             return UniqueInstance;
         }
         UniqueInstance=constrotor();
         return UniqueInstance;
     }
     return getUniqueInstance();
 }();
 //腳本執行時間
 /*使用方法
 var ti=new YYJ.ticks();
 ti.begin();
 代碼段
 ti.end();
 alert(ti.tick);*/
 YYJ.ticks=function(){
     var starttick,stoptick;
     return function(){
         this.begin=function(){
             starttick=new Date();
         }
         this.end=function(){
             stoptick=new Date();
             this.tick=stoptick-starttick;
         }
     }
 }();
XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved