zl程序教程

您现在的位置是:首页 >  前端

当前栏目

javascript工具库代码

2023-06-13 09:14:33 时间
复制代码代码如下:

/*
 *YYJ.js主要提供后台应用方法
 *目前有Ajax、addLoadEvent、deleteAll、getByClass、$C(=getByClass)、$、yyjtable、setCss、getCss、iaArray等方法。最后面还提供了一个
 *可实例化的方法YYJ.tick用来计算脚本运行时间
 *版本0.1   --tianyi   yyj
 */
 varYYJ=function(){
    varUniqueInstance;
    functionconstrotor(){
        return{
            //ajax方法
            Ajax:function(method,url,data,success,fail){
                varisget=method.toLowerCase()=="get";
                varxmlhttp=window.XMLHttpRequest?newXMLHttpRequest():newActiveXObject("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){
                varoldonload=window.onload;
                if(typeofwindow.onload!="function"){
                    window.onload=func;
                }else{
                    window.onload=function(){
                      if(oldonload){
                        oldonload();
                      }
                      func();
                    }
                }
            },//addLoadEvent
            deleteAll:function(checkallbtn,checksomebtn,submitbtn){
                varcheckall=YYJ.$(checkallbtn);
                varchecksome=YYJ.$(checksomebtn);
                varsubmit1=YYJ.$(submitbtn);
                varcheckboxs=(function(){
                    vararr=[];
                    varcheck=document.getElementsByTagName("input");
                    for(i=0;i<check.length;i++){
                        if(check[i].getAttribute("type")!="checkbox")
                            continue;
                        arr.push(check[i]);
                    }
                    returnarr;
                })();
                checkall.onclick=function(){
                    checkall.clicked=true;
                    for(vari=0;i<checkboxs.length;i++){
                        if(!checkboxs[i].checked){
                            checkall.clicked=false;
                            break;
                        }
                    }
                    if(!checkall.clicked){
                        for(vari=0;i<checkboxs.length;i++){
                            checkboxs[i].checked=true;
                        }
                    }else{
                        for(vari=0;i<checkboxs.length;i++){
                            checkboxs[i].checked=false;
                        }
                    }
                }
                checksome.onclick=function(){
                    for(varxincheckboxs){
                        checkboxs[x].checked=!checkboxs[x].checked;
                    }
                }
                submit1.onclick=function(){
                    varhaschecked=false;
                    varbeSureDel=false;
                    for(vari=0;i<checkboxs.length;i++){
                        if(checkboxs[i].checked){
                            haschecked=true;
                            break;
                        }
                    }
                    haschecked?function(){
                        beSureDel=confirm("确定要删除选中项吗?");
                    }():function(){
                        alert("没有选中项!");
                        beSureDel=false;
                    }();
                    returnbeSureDel;
                }
            },//deleteall
            getByClass:function(classname,parent,nodename){
                vars=(parent||document).getElementsByTagName(nodename||"*");
                returnfunction(){
                    vara=[];
                    for(vari=0,j=s.length;i<j;i++){
                        if(!s[i].className)continue;
                        varname=""+s[i].className+"";
                        if(name.indexOf(""+classname+"")!=-1){
                            a.push(s[i]);
                        }
                    }
                    returna;
                }();
            },
            $C:function(classname,parent,nodename){
                returnYYJ.getByClass(classname,parent,nodename);
            },//getByClass
            $:function(str){
                returndocument.getElementById(str);
            },//getById
            yyjtable:function(tableId){
                vartbl=YYJ.$(tableId);
                vartrs=tbl.getElementsByTagName("tr");
                for(vari=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){
                    thrownewError("setCss的第一个参数要求为数组!");
                }
                for(vari=0;j=eles[i];i++){
                    try{
                        for(varxinopt){
                            j.style[x]=opt[x];
                        }
                    }catch(ex){}
                }
            },//setCss
            /*使用方法
            varcss=YYJ.getCss(YYJ.$("table1"),["backgroundColor"]);
            alert(css["backgroundColor"]);*/
            getCss:function(ele,opt){
                if(!this.isArray(opt)){
                    thrownewError("getCss的第二个参数要求为string数组!");
                }
                varcss={};
                for(vari=0,j=opt.length;i<j;i++){
                    try{
                        css[opt[i]]=ele.style[opt[i]];
                    }catch(ex){}
                }
                returncss;

            },//getCss
            isArray:function(opt){
                returnObject.prototype.toString.call(opt)=="[objectArray]"
            }

        };
    }
    functiongetUniqueInstance(){
        if(UniqueInstance){
            returnUniqueInstance;
        }
        UniqueInstance=constrotor();
        returnUniqueInstance;
    }
    returngetUniqueInstance();
 }();
 //脚本执行时间
 /*使用方法
 varti=newYYJ.ticks();
 ti.begin();
 代码段
 ti.end();
 alert(ti.tick);*/
 YYJ.ticks=function(){
    varstarttick,stoptick;
    returnfunction(){
        this.begin=function(){
            starttick=newDate();
        }
        this.end=function(){
            stoptick=newDate();
            this.tick=stoptick-starttick;
        }
    }
 }();