javascript常用的方法整理
整理了一些JS的常用方法,包括验证啊,全选反选啊,ajax请求啊之类的,因为就是自己用的,写的都比较简单,就算抛砖引玉吧,喜欢的就拿去,不喜欢就拉到
Tools.min.js
/**
*JS公用类库文件
*创建时间:2015-05-13
*创建人:mction
*/
(function(){
varD=document;
varW=window;
varPostfix='.php'
var_Id=function(Id){returndocument.getElementById(Id);};
Check={
Input:function(Name,Value,Message){
varInput=$(":input[name='"+Name+"']");
if(Input.val()==Value){
Input.focus();
alert(Message);
returntrue;
}
returnfalse;
},
Phone:function(Name){
/*
*联通号段:130/131/132/155/156/185/186/145/176;
*电信号段:133/153/180/181/189/177;
*移动号段:134/135/136/137/138/139/150/151/152/157/158/159/182/183/184/187/188/147/178
*/
varReg=/^1(30|31|32|55|56|85|86|45|76|33|53|80|81|89|77|34|35|36|37|38|39|50|51|52|57|58|59|82|83|84|87|88|47|78)[0-9]{8}$/;
varPhone=$(":input[name='"+Name+"']");
if(!Reg.test(Phone.val())){
Phone.focus();
returntrue;
}
returnfalse;
},
Email:function(Name){
varReg=/^[a-zA-Z0-9_]+@[a-z0-9]+\.[a-z]+$/;
varEmail=$(":input[name='"+Name+"']");
if(!Reg.test(Email.val())){
Email.focus();
returntrue;
}
returnfalse;
},
UserName:function(UserNameMark,Message){
varReg=/^[\d]{8}$/;
varUserName=$(":input[name='"+UserNameMark+"']");
if(!Reg.test(UserName.val())){
UserName.focus();
alert(Message);
returntrue;
}
returnfalse;
},
Password:function(Name){
varReg=/^([A-Z]+)$|^([a-z]+)$|^([0-9]+)$|^([-`=\\\[\];',\.\/~!@#\$%\^&\*\(\)_+\|\{}:"<>\?]+)$|^.{0,5}$|^.{18,}$/
varPassword=$(":input[name='"+Name+"']");
if(Reg.test(Password.val())){
Password.focus();
returntrue;
}else{
returnfalse;
}
}
};
Member={
Login:function(){
varUserName=$(":input[name='username']");
varPassWord=$(":input[name='password']");
if(Check.UserName("username","用户名格式不正确")){
returnfalse;
}
if(Check.Input("password",'',"密码不能为空")){
returnfalse;
}
$.ajax({
url:"/User/action"+Postfix,
type:"POST",
data:{
request:"Login",
username:UserName.val(),
password:PassWord.val()
},
dataType:"json",
success:function(Data){
if(Data.state!=200){
alert(Data.message);
returnfalse;
}else{
location.href="/User/member"+Postfix;
}
}
});
},
Logout:function(){
location.href='/User/Logout'+Postfix;
}
};
Public={
Hi:function(){alert('hi');},
Box_All_Sel:function(Class,AllChecked){//全选反选
varInput=D.getElementsByTagName("input");
varBoxList=[];
for(I=0;I<Input.length;I++){
if(Input[I].type=="checkbox"&&Input[I].className==Class){
BoxList.push(Input[I]);
}
}
if(AllChecked){
for(IinBoxList){
BoxList[I].checked=true;
}
}else{
for(IinBoxList){
BoxList[I].checked=BoxList[I].checked?false:true;
}
}
},
After:function(New,Tar){
if(typeofTar=='string'){
varParent=_Id(Tar);
}else{
varParent=Tar;
}
if(Parent.parentNode==Parent){
Parent.parentNode.appendChild(New);
}else{
Parent.parentNode.insertBefore(New,Parent.nextSibling);
}
},
Requests:function(O,Class){
//批量请求
//url.request.message.input.inputMessage.inputValue
varData=$("."+Class+":checked").serialize();
if(Data==''){
alert("您没有选中任何项");
//alert(O.options[0].value);
O.value=O.options[0].value;
return;
}
varTempArr=O.value.split('.');
if(!TempArr[0]||!TempArr[1]){
//验证URL及动作
alert("错误:缺少必须参数");
O.value=O.options[0].value;
return;
}
Data+="&request="+TempArr[1];
varMessage="确认删除选中项吗?";
if(TempArr[2]){
//验证并设置提示消息
Message=TempArr[2];
}
if(confirm(Message)){
varInput=false;
if(TempArr[3]){
//验证并设置是否接收用户输入
Input=true;
}
if(Input=='True'){
varInputVal=prompt(TempArr[4],TempArr[5]);
Data+="&input="+InputVal;
}
$.ajax({
url:"./"+TempArr[0]+Postfix,
type:'GET',
data:Data,
dataType:'json',
async:false,
success:function(Data){
alert(Data.message);
location.reload(true);
}
});
}
},
Request:{//单次请求
Data:'',
MetHod:'',
DataType:'',
Async:'',
SetData:function(Options,MetHod,DataType,Async){
this.Data=Options;
this.MetHod=typeofMetHod=='undefined'?'GET':MetHod;
this.DataType=typeofDataType=='undefined'?'json':DataType;
this.Async=typeofAsync=='undefined'?true:Async;
returnthis;
},
Send:function(Url,Call,IM,Message){
if(!Message){
Message="确认删除吗?";
}
if(typeofCall!='function'){
Call=function(Data){
switch(this.dataType){
case'text':alert(Data);break;
default:alert(Data.message);break
}
location.reload(true);
}
}
varCF=true;
varCFM=true;
varLId;
if(typeofIM!='undefined'&&IM==false){CF=false}
if(CF&&!confirm(Message)){CFM=false;}
if(CFM){
$.ajax({
url:Url,
type:this.MetHod,
data:this.Data,
dataType:this.DataType,
async:this.Async,
beforeSend:function(O){LId=Public.Loading.Open("正在处理中");},
success:Call,
error:function(E,Info,EO){alert(E.statusText+":"+E.responseText);},
complete:function(O){Public.Loading.Close(LId);}
});
}
}
},
Desc:{
DescId:'',
Display:function(O,Message){
varId=parseInt(Math.random()*1000);
this.DescId=Id;
varDesc=D.createElement('description');
Desc.id=Id;
Desc.innerHTML=Message;
Desc.style.width="200px";
Desc.style.border="1pxsolid#dfdfdf";
Desc.style.backgroundColor="#fff";
Desc.style.lineHeight="normal";
Desc.style.position="absolute";
Desc.style.top=O.offsetTop+'px';
Desc.style.marginLeft="5px";
Public.After(Desc,O);
O.setAttribute("onblur",'Public.Desc.Close()');
},
Close:function(){
$("#"+this.DescId).remove();
}
},
Options:{
File_Input:function(Obj){//添加文件选择框
varchars='ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678';
varObjName='';
for(vari=0;i<5;i++){
ObjName+=chars.charAt(Math.floor(Math.random()*chars.length));
}
varInputFile=D.createElement("input");
InputFile.type="file";
InputFile.name=ObjName;
InputFile.accept="image/*";
varA=D.createElement("a");
A.href="javascript:;";
A.style.marginLeft="300px";
A.innerHTML="删除";
A.setAttribute("onclick",'$("div").remove("#'+ObjName+'")');
varDiv=D.createElement("div");
Div.id=ObjName;
Div.style.padding="5px";
Div.style.borderBottom="1pxsolid#cccccc";
Public.After(Div,Obj);
_Id(ObjName).appendChild(InputFile);
_Id(ObjName).appendChild(A);
}
},
Loading:{
WindowId:'',
Open:function(Message){
varId=parseInt(Math.random()*1000);
this.WindowId=Id;
varDiv=D.createElement("div");
Div.id=Id;
Div.style.width="80%";
Div.style.height="50px";
Div.style.backgroundColor="#000";
Div.style.opacity="0.5";
Div.style.borderRadius="10px";
Div.style.position="fixed";
Div.style.top=(W.innerHeight-50)/2+"px";
Div.style.left="10%";
varP=D.createElement("p");
P.style.textAlign="center";
P.style.color="#fff";
P.style.lineHeight="50px";
P.style.height="50px";
varImg=D.createElement('img');
Img.src="/phps/Public/images/loading.gif";
Img.style.marginBottom="-5px";
if(Message){
P.innerHTML=Message;
}else{
P.innerHTML="正在处理中";
}
P.appendChild(Img);
D.getElementsByTagName("body")[0].appendChild(Div);
_Id(this.WindowId).appendChild(P);
returnId;
},
Close:function(Id){
if(typeofId!='undefined'&&Id!=null){
D.getElementsByTagName("body")[0].removeChild(_Id(Id));
}elseif(this.WindowId!=''){
D.getElementsByTagName("body")[0].removeChild(_Id(this.WindowId));
}
}
},
Tabs:{
TabsNum:1,
TabListId:'',
WindowId:'',
New_Tabs:function(TabListId,WindowId,OpenUrl,PageTitle){
this.TabListId=TabListId;
this.WindowId=WindowId;
varIframeList=_Id(WindowId).getElementsByTagName("iframe");
//alert(Iframe.length);
for(I=0;I<IframeList.length;I++){
IframeList[I].style.display="none";
}
varTabList=_Id(TabListId).getElementsByTagName("li");
//alert(TabList);
for(I=0;I<TabList.length;I++){
TabList[I].className="";
}
varIframe=D.createElement("iframe");
Iframe.id="IFR"+this.TabsNum;
Iframe.src=OpenUrl;
Iframe.frameborder=0;
Iframe.width="100%";
Iframe.height="100%";
varA=D.createElement("a");
A.href="javascript:;";
A.id="IFA"+this.TabsNum;
A.innerHTML=PageTitle;
A.setAttribute("data-if",Iframe.id);
A.setAttribute("onclick","Public.Tabs.View(this)");
varLi=D.createElement("li");
Li.id="IFL"+this.TabsNum;
Li.className="on";
Li.setAttribute("data-if",Iframe.id);
varClose=D.createElement("a");
Close.href="javascript:;";
Close.innerHTML="x";
Close.setAttribute("data-li",Li.id);
Close.setAttribute("data-if",Iframe.id);
Close.setAttribute("onclick","Public.Tabs.Close(this)");
this.TabsNum++;
_Id(TabListId).appendChild(Li);
_Id(Li.id).appendChild(A);
_Id(Li.id).appendChild(Close);
_Id(WindowId).appendChild(Iframe);
},
View:function(O){
varIframeList=_Id(this.WindowId).getElementsByTagName("iframe");
for(I=0;I<IframeList.length;I++){
IframeList[I].style.display="none";
}
varTabList=_Id(this.TabListId).getElementsByTagName("li");
for(I=0;I<TabList.length;I++){
TabList[I].className="";
}
O.parentNode.className="on";
_Id(O.getAttribute('data-if')).style.display="block";
},
Close:function(O){
varLiO=_Id(O.getAttribute("data-li"));
varIFO=_Id(O.getAttribute("data-if"));
if(LiO.nextElementSibling){
varDisPlayLi=LiO.nextElementSibling;
}else{
varTabList=_Id(this.TabListId).getElementsByTagName("li");
varDisPlayLi=TabList[TabList.length-2];
}
if(IFO.nextElementSibling){
varDisPlayIF=IFO.nextElementSibling;
}else{
varIfList=_Id(this.WindowId).getElementsByTagName("iframe");
varDisPlayIF=_Id(IfList[IfList.length-2].id);
}
LiO.parentNode.removeChild(LiO);
IFO.parentNode.removeChild(IFO);
DisPlayLi.className="on";
DisPlayIF.style.display="block";
}
}
};
})();
这些是JAVASCRIPT的基本知识,如果是新手的话,我觉得这些不够你们学习,你可以去找些资料完整的学习下JAVASCRIPT。
这些对于已经有一些JAVASCRIPT基础的朋友,可以先浏览下这些知识,或许其中有你已经遗忘的或者遗漏的,回忆下JAVASCRIPT对你后面继续深入学下AJAX有很大的帮助。