Heim >Web-Frontend >js-Tutorial >一些常用的Javascript函数_javascript技巧
来公司后所在项目组开发的是一个客户端的软件,软件界面采用WEB界面来体现,因此要用到不少Javascript.来公司后所在项目组开发的是一个客户端的软件,软件界面采用WEB界面来体现,因此要用到不少Javascript.把自己写的一些通用JS函数整理了下:)
/*判断浏览器版本*/
var w3c=(document.getElementById)? true: false;
var agt=navigator.userAgent.toLowerCase();
var ie = ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1) && (agt.indexOf("omniweb") == -1));
var ie5=(w3c && ie)? true : false;
var ns6=(w3c && (navigator.appName=="Netscape"))? true: false;
var op8=(navigator.userAgent.toLowerCase().indexOf("opera")==-1)? false:true;
function Ob(o){//取得一个对象
return document.getElementById(o)?document.getElementById(o):o;
}
function IsSubStr(p,s){//判断是否为子字符串
return (p.indexOf(s)>-1);
}
function Hd(o){//隐藏某个对象
Ob(o).style.display="none";
}
function Sw(o){//显示某个对象
Ob(o).style.display="block";
}
function Sw2(o){//显示某个对象
Ob(o).style.display="";
}
function Sf(o,s,b){
//该函数用于多按钮共用同一表单,其中:o为表单的ID b为隐藏的input的ID(其value供后如判断用户提交动作类型) s为用户提交动作类型
Ob(b).value=s;
Ob(o).submit();
}
function ExChgCls(o,a,b){//切换对象的className
o.className=o.className==a?b:a;
}
function ExChgHtml(o,a,b){//切换对象的innerHTML
o.innerHTML=o.innerHTML==a?b:a;
}
function Oh(o,s){ //输出字符串给某个对象
Ob(o).innerHTML=s;
}
function SetSrc(o,s){//改变img、iframe等对象的src值
Ob(o).src=s;
}
/*XmlHttp*/
function Gp(url,o){ //GetHttpPage
var o=Ob(o);
var PageRequest = false;
IsLoading(o);
if (window.XMLHttpRequest) {// if Mozilla, Safari etc
PageRequest = new XMLHttpRequest();
}
else if (window.ActiveXObject){ // if IE
try {
PageRequest = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e){
try{
PageRequest = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e){;}
}
}
else {return false ;}
PageRequest.onreadystatechange=function(){
if (PageRequest.readyState == 4) {
Lp(PageRequest,o);
}
}
PageRequest.open('GET', url, true);
PageRequest.send(null);
}
function Lp(PageRequest,o){ //LoadPage
if (PageRequest.readyState == 4 && (PageRequest.status==200 || window.location.href.indexOf("http")==-1))
Ob(o).innerHTML=PageRequest.responseText;
//Hd("OpMsg");
}
function IsLoading(o){
o.innerHTML="数据加载中,请稍候......";
//Sw2("OpMsg");
}
//Include Javascript File
function IncJs(sSrc,sID){
var oHead = document.getElementsByTagName('head')[0];
var oScript = document.createElement('script');
oScript.type = "text/javascript";
oScript.src = sSrc;
oScript.id = sID;
if(oScript.readyState=="loaded") {oHead.appendChild(oScript); }
}
/*Move & Remove*/
function Mv(a,b,n) {
var i = 0;
var f=Ob(a);
var t=Ob(b);
var n=n?n:0;
if (n==0) {
for(var ii=0; ii
var no = new Option();
no.value = f.options[ii].value;
no.text = f.options[ii].text;
t.options[t.options.length] = no;
}
}
}
else {
for(var ii=0; ii
t.value += ","+f.options[ii].value;
}
}
}
Rv(a);
}
function Rv(o) {
var o=Ob(o);
for(var ii=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;
}
}
}
function ChgInp(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*/
function DoChk(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);
}
}
function IsAllChk(o,s){
var ChkNums=0;
var arrObj=Ob(o).getElementsByTagName("input");
this.OldCls = s || "";
for (i=0;i
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;
}
}
function ChkAll(o,o2,s){
var arrObj=Ob(o2).getElementsByTagName("input");
this.OldCls = s || "";
for (ii=1;ii
if (o.checked) {
arrObj[ii].parentNode.parentNode.className="chked";
} else {
arrObj[ii].parentNode.parentNode.className=this.OldCls;
}
}
}
function OppChk(){
var arrObj=Ob("Photos").getElementsByTagName("input");
for (ii=0;ii
}
IsAllChk();
}
/*Test Nums*/
function IsIntNum(s){
var reIntNum=/^\-?\d+$/;
return reIntNum.test(s);
}
function IsBetween(n0,n1,n2,t){
this.CompareType=t || 4;
switch (this.CompareType) {
case 0 :
return (n0>n1 && n0
return (n0>=n1 && n0
return (n0>n1 && n0 default :
return (n0>=n1 && n0 }
}