一些常用的Javascript函数

2018-10-15 18:04

阅读:650

来公司后所在项目组开发的是一个客户端的软件,软件界面采用WEB界面来体现,因此要用到不少Javascript.来公司后所在项目组开发的是一个客户端的软件,软件界面采用WEB界面来体现,因此要用到不少Javascript.把自己写的一些通用JS函数整理了下:)

/*判断浏览器版本*/
varw3c=(document.getElementById)?true:false;
varie5=(w3c&&ie)?true:false;
varns6=(w3c&&(navigator.appName==Netscape))?true:false;
varop8=(navigator.userAgent.toLowerCase().indexOf(opera)==-1)?false:true;

functionOb(o){//取得一个对象
returndocument.getElementById(o)?document.getElementById(o):o;
}

functionIsSubStr(p,s){//判断是否为子字符串
return(p.indexOf(s)>-1);
}

functionHd(o){//隐藏某个对象
Ob(o).style.display=none;
}

functionSw(o){//显示某个对象
Ob(o).style.display=block;
}
functionSw2(o){//显示某个对象
Ob(o).style.display=;
}

functionSf(o,s,b){
//该函数用于多按钮共用同一表单,其中:o为表单的IDb为隐藏的input的ID(其value供后如判断用户提交动作类型)s为用户提交动作类型
Ob(b).value=s;
Ob(o).submit();
}

functionExChgCls(o,a,b){//切换对象的className
o.className=o.className==a?b:a;
}

functionExChgHtml(o,a,b){//切换对象的innerHTML
o.innerHTML=o.innerHTML==a?b:a;
}

functionOh(o,s){//输出字符串给某个对象
Ob(o).innerHTML=s;
}

functionSetSrc(o,s){//改变img、iframe等对象的src值
Ob(o).src=s;
}

/*XmlHttp*/
functionGp(url,o){//GetHttpPage
varo=Ob(o);
varPageRequest=false;
IsLoading(o);
if(window.XMLHttpRequest){//ifMozilla,Safarietc
PageRequest=newXMLHttpRequest();
}
elseif(window.ActiveXObject){//ifIE
try{
PageRequest=newActiveXObject(Msxml2.XMLHTTP);
}
catch(e){
try{
PageRequest=newActiveXObject(Microsoft.XMLHTTP);
}
catch(e){;}
}
}
else{returnfalse;}
PageRequest.onreadystatechange=function(){
if(PageRequest.readyState==4){
Lp(PageRequest,o);
}
}
PageRequest.open(GET,url,true);
PageRequest.send(null);
}

functionLp(PageRequest,o){//LoadPage
if(PageRequest.readyState==4&&(PageRequest.status==200window.location.href.indexOf(http)==-1))
Ob(o).innerHTML=PageRequest.responseText;
//Hd(OpMsg);
}

functionIsLoading(o){
o.innerHTML=数据加载中,请稍候......;
//Sw2(OpMsg);
}

//IncludeJavascriptFile
functionIncJs(sSrc,sID){
varoHead=document.getElementsByTagName(head)[0];
varoScript=document.createElement(script);
oScript.type=text/javascript;
oScript.src=sSrc;
oScript.id=sID;
if(oScript.readyState==loaded){oHead.appendChild(oScript);}
}

/*Move&Remove*/
functionMv(a,b,n){
vari=0;
varf=Ob(a);
vart=Ob(b);
varn=n?n:0;
if(n==0){
for(varii=0;ii<f.options.length;ii++){
if(f.options[ii].selected){
varno=newOption();
no.value=f.options[ii].value;
no.text=f.options[ii].text;
t.options[t.options.length]=no;
}
}
}
else{
for(varii=0;ii<f.options.length;ii++){
if(f.options[ii].selected){
t.value+=,+f.options[ii].value;
}
}
}
Rv(a);
}

functionRv(o){
varo=Ob(o);
for(varii=o.options.length-1;ii>=0;ii--){
if(o.options[ii].selected&&o.options[ii]!=){
o.options[ii].value=;
o.options[ii].text=;
o.options[ii]=null;
}
}
}

functionChgInp(o1,o2){
Ob(o2).readOnly=(!Ob(o1).checked);
if(Ob(o1).checked){
Ob(o2).className=InpTxt;
Ob(o2).focus();
}else{
Ob(o2).className=InpTxt0;
}
}

/*Checkbox*/
functionDoChk(o,o2,s){
this.OldCls=s;
if(o.checked==false){
o.parentNode.parentNode.className=this.OldCls;
Ob(BtnChkAll).checked=false;
}else{
o.parentNode.parentNode.className=chked;
IsAllChk(o2,this.OldCls);
}
}

functionIsAllChk(o,s){
varChkNums=0;
vararrObj=Ob(o).getElementsByTagName(input);
this.OldCls=s;
for(i=0;i<arrObj.length;i++){
if(arrObj[ii].checked){
ChkNums+=1;
arrObj[ii].parentNode.parentNode.className=chked;
}else{
arrObj[ii].parentNode.parentNode.className=this.OldCls;
}
}
//alert(ChkNums+:+arrObj.length)
if(ChkNums==arrObj.length-1){
Ob(BtnChkAll).checked=true;
}else{
Ob(BtnChkAll).checked=false;
}
}

functionChkAll(o,o2,s){
vararrObj=Ob(o2).getElementsByTagName(input);
this.OldCls=s;
for(ii=1;ii<arrObj.length;ii++){
arrObj[ii].checked=o.checked;
if(o.checked){
arrObj[ii].parentNode.parentNode.className=chked;
}else{
arrObj[ii].parentNode.parentNode.className=this.OldCls;
}
}
}

functionOppChk(){
vararrObj=Ob(Photos).getElementsByTagName(input);
for(ii=0;ii<arrObj.length;ii++){
arrObj[ii].checked=!arrObj[ii].checked;
}
IsAllChk();
}

/*TestNums*/
functionIsIntNum(s){
varreIntNum=/^\-?\d+$/;
returnreIntNum.test(s);
}
functionIsBetween(n0,n1,n2,t){
case0:
return(n0>n1&&n0<n2);
case1:
return(n0>=n1&&n0<n2);
case2:
return(n0>n1&&n0<=n2);
default:
return(n0>=n1&&n0<=n2);
}
}


评论


亲,登录后才可以留言!