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+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;
         }
     }
 }();

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wdjzjw.html