复制代码 代码如下:
/*
*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 if(check[i].getattribute(type)!=checkbox)
continue;
arr.push(check[i]);
}
return arr;
})();
checkall.onclick=function(){
checkall.clicked=true;
for(var i=0;i if(!checkboxs[i].checked){
checkall.clicked=false;
break;
}
}
if(!checkall.clicked){
for(var i=0;i checkboxs[i].checked=true;
}
}else{
for(var i=0;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 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 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 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 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;
}
}
}();