平常用到的一些js代码



/*
标准程序块
Author:KingAstar
Date:2005-11-10
*/


/******************************************************
Desp:调用远程数据
Date:2005-11-9
Author:Kingastar
Params:改变的本地Div或者其它元素的ID,远程调用的URL,处理函数
如果处理函数为空,则自动将返回的HTML代码设置为元素内部的HTML
******************************************************
*/

functionLoadDataInContainer(objeName,url,dealFunc)
{


this.ContentObj=document.getElementById(objeName);
if(this.ContentObj==null)
return;

ShowProgress(
1);


if(dealFunc!=null)
this.DealFunction=dealFunc;
else
this.DealFunction=null;


if(window.XMLHttpRequest){
req
=newXMLHttpRequest();
req.onreadystatechange
=processReqChange;
req.open(
"GET",url,true);
req.send(
null);
//branchforIE/WindowsActiveXversion
}
elseif(window.ActiveXObject){
isIE
=true;
req
=newActiveXObject("Microsoft.XMLHTTP");
if(req){
req.onreadystatechange
=processReqChange;
req.open(
"GET",url,true);
req.send();
}

}



}

//数据返回后处理
functionprocessReqChange(){
//onlyifreqshows"loaded"
if(req.readyState==4){
//onlyif"OK"
if(req.status==200)
{
if(this.DealFunction!=null)
{
try
{
this.DealFunction();
}
catch(e){alert("调用方法失败:"+e.description);}
ShowProgress(
2);



}

else
{
//alert(req.responseText);
try
{
this.ContentObj.innerHTML=req.responseText;
}

catch(e){alert(e.description);}
ShowProgress(
2);

}

}
else{
alert(
"TherewasaproblemretrievingtheXMLdata:\n"+
req.statusText);
}

}

}

//显示进程正在进行
functionShowProgress(status)
{

varprogressDiv=document.getElementById("progressDiv");
if(progressDiv==null)
{
progressDiv
=document.createElement("div");
progressDiv.innerHTML
="数据加载中";
progressDiv.id
="progressDiv";
progressDiv.name
="progressDiv";
document.body.appendChild(progressDiv);
}


if(status==1)
progressDiv.style.display
="";
else
progressDiv.style.display
="none";

}








/******************************************************
Desp:点击设置表格颜色的程序块
Date:2005-11-9
Author:Kingastar
Params:table,新颜色代码,是否包括table的题头也变
*****************************************************
*/


functionAttachTableOnchangeEvent(table,NewColor,includingTitle)
{

this.newColor=NewColor;
this.includingtitle=includingTitle;
table.attachEvent(
"onmousedown",OnClickChangeTdBackColor);
}

functionOnClickChangeTdBackColor()
{

varsrcElement=event.srcElement;
//varEverselected="whitesmoke";
varTabElement=event.srcElement;
varoldcolor;
  
while(srcElement.tagName.toLowerCase()!="tr")
  
{
   srcElement
=srcElement.parentElement;
  }

  
  
  
while(TabElement.tagName.toLowerCase()!="table")
  
{
  TabElement
=TabElement.parentElement;
  }

  
//alert(srcElement.tagName);
  
  
  
for(vari=0;i<TabElement.rows.length;i++)
  
{
  
if(TabElement.rows[i].style.background!=this.newColor)
  
{
  oldcolor
=TabElement.rows[i].style.background;
  
break;
  }

  
  }

  
for(vari=0;i<TabElement.rows.length;i++)
  
{
  
  TabElement.rows[i].style.background
=oldcolor;
  }

  
  
//alert(srcElement.rowIndex==0&&this.includingtitle);
if(!(srcElement.rowIndex==0&&!this.includingtitle))
 srcElement.style.background
=this.newColor;

  
  
  
}


/******************************************************
Desp:取得当前URL地址
Date:2005-11-11
Author:Kingastar
Params:其它url参数
Returns:当前的路径+其它url参数
*****************************************************
*/

functiongetVirtualPath(reUrl)
{

varss,sss;
try
{
ss
=window.location;
}

catch(e)
{
alert(e.description);
}

ss
+="";

sss
=ss.split("/");
ss
="";
if(sss.length>1)
{
for(i=0;i<sss.length-1;i++)
{
ss
+=sss[i]+"/";
}

}

returnss+reUrl;
}






/******************************************************
Desp:Animplementationofthedebuglog.
Date:2005-11-15
Author:googleinc
*****************************************************
*/

varlogging__=false;
functionLog(){};

Log.lines
=[];

Log.write
=function(s){
if(logging__){
this.lines.push(xmlEscapeText(s));
this.show();
}

}
;

//WritesthegivenXMLwitheverytagonanewline.
Log.writeXML=function(xml){
if(logging__){
vars0=xml.replace(/</g,'\n<');
vars1=xmlEscapeText(s0);
vars2=s1.replace(/\s*\n(\s|\n)*/g,'<br/>');
this.lines.push(s2);
this.show();
}

}


//Writeswithoutanyescaping
Log.writeRaw=function(s){
if(logging__){
this.lines.push(s);
this.show();
}

}


Log.clear
=function(){
if(logging__){
varl=this.div();
l.innerHTML
='';
this.lines=[];
}

}


Log.show
=function(){
varl=this.div();
l.innerHTML
+=this.lines.join('<br/>')+'<br/>';
this.lines=[];
l.scrollTop
=l.scrollHeight;
}


Log.div
=function(){
varl=document.getElementById('log');
if(!l){
l
=document.createElement('div');
l.id
='log';
l.style.position
='absolute';
l.style.right
='10px';
l.style.top
='20px';
l.style.width
='250px';
l.style.height
='150px';
l.style.overflow
='auto';
l.style.backgroundColor
='#f0f0f0';
l.style.border
='1pxsolidgray';
l.style.fontSize
='10px';
l.style.padding
='5px';
document.body.appendChild(l);
}

returnl;
}


/*
Desp:产生下拉列过滤选择项;Attachthefiltertoatable.
Date:2005-11-9
Author:?
Params:
filterRowspecifiestherownumberatwhichthefiltershouldbeinserted.
Modify:
主要比较的是内容,所有把innerHTML换成了innerText
另外就是在特定的列创建
2.0
*/


functionattachFilter2(table,filterRow)
{

table.filterRow
=filterRow;

//Checkifthetablehasanyrows.Ifnot,donothing
if(table.rows.length>0)
{
//Insertthefilterrowandaddcellswhithdrowdowns.
varfilterRow=table.insertRow(table.filterRow);

table.fillFilters
=fillFilters;
table.inFilter
=inFilter;
table.buildFilter
=buildFilter;
table.showAll
=showAll;
table.detachFilter
=detachFilter;
table.filterElements
=newArray();
table.filterElements[
0]=2;
varcolLen=table.rows[0].cells.length;
alert(colLen);


for(vari=0;i<table.rows[table.filterRow+1].cells.length;i++)
{
varc=document.createElement("TH");
table.rows[table.filterRow].appendChild(c);
varopt=document.createElement("select");
opt.onchange
=filter;

if(!table.inFilter(colLen-i))
{

c.appendChild(opt);
}

else
{
c.innerText
="";
}

}

//Setthefunctions




//Fillthefilters
table.fillFilters();
table.filterEnabled
=true;
}

}


functiondetachFilter()
{
if(this.filterEnabled)
{
//Removethefilter
this.showAll();
this.deleteRow(this.filterRow);
this.filterEnabled=false;
}

}


//Checksifacolumnisfiltered
functioninFilter(col)
{
for(vari=0;i<this.filterElements.length;i++)
{
//if(this.filterElements[i].index==col)
if(this.filterElements[i]==col)
returntrue;
}

returnfalse;
}


//Fillsthefiltersforcolumnswhicharenotfiiltered
functionfillFilters()
{

for(varcol=0;col<this.rows[this.filterRow].cells.length;col++)
{
if(!this.inFilter(col))
{
this.buildFilter(col,"(all)");
}

}




}


//Fillsthecolumnsdropdownbox.
//
setValueisthevaluewhichthedropdownboxshouldhaveonefilled.
//
Ifthevalueisnotsuplied,thefirstitemisselected
functionbuildFilter(col,setValue)
{

//Getareferencetothedropdownbox.
varopt=this.rows[this.filterRow].cells[col].firstChild;

//removeallexistingitems
while(opt.length>0)
opt.remove(
0);

varvalues=newArray();

//putallrelevantstringsinthevaluesarray.
for(vari=this.filterRow+1;i<this.rows.length;i++)
{
varrow=this.rows[i];
if(row.style.display!="none"&&row.className!="noFilter")
{
values.push(row.cells[col].innerText.toLowerCase());
}

}

values.sort();

//addeachuniquestringtothedopdownbox
varvalue;
for(vari=0;i<values.length;i++)
{
if(values[i].toLowerCase()!=value)
{
value
=values[i].toLowerCase();
opt.options.add(
newOption(values[i],value));
}

}


opt.options.add(
newOption("(all)","(all)"),0);

if(setValue!=undefined)
opt.value
=setValue;
else
opt.options[
0].selected=true;
}


//Thisfunctioniscalledwhenadropdownboxchanges
functionfilter()
{
vartable=this;//'this'isareferencetothedropdownboxwhichchanged
while(table.tagName.toUpperCase()!="TABLE")
table
=table.parentNode;

varfilterIndex=this.parentNode.cellIndex;//Thecolumnnumberofthecolumnwhichshouldbefiltered
varfilterText=table.rows[table.filterRow].cells[filterIndex].firstChild.value;

//Firstcheckifthecolumnisallreadyinthefilter.
varbFound=false;

for(vari=0;i<table.filterElements.length;i++)
{
if(table.filterElements[i].index==filterIndex)
{
bFound
=true;
//Ifthenewvalueis'(all')thiscolumnisremovedfromthefilter.
if(filterText=="(all)")
{
table.filterElements.splice(i,
1);
}

else
{
table.filterElements[i].filter
=filterText;
}

break;
}

}

if(!bFound)
{
//thecolumnisaddedtothefilter
varobj=newObject();
obj.filter
=filterText;
obj.index
=filterIndex;
table.filterElements.push(obj);
}


//firstsetallrowstobedisplayed
table.showAll();

//thefilteroutherightrows.
for(vari=0;i<table.filterElements.length;i++)
{
//Firstfillthedropdownboxforthiscolumn
table.buildFilter(table.filterElements[i].index,table.filterElements[i].filter);
//Applythefilter
for(varj=table.filterRow+1;j<table.rows.length;j++)
{
varrow=table.rows[j];

if(table.style.display!="none"&&row.className!="noFilter")
{
if(table.filterElements[i].filter!=row.cells[table.filterElements[i].index].innerText.toLowerCase())
{
row.style.display
="none";
}

}

}

}

//Fillthedropdownboxesfortheremainingcolumns.
table.fillFilters();
}


functionshowAll()
{
for(vari=this.filterRow+1;i<this.rows.length;i++)
{
this.rows[i].style.display="";
}

}

优质内容筛选与推荐>>
1、Java分支结构 - if...else/switch
2、UVALive 7456 Least Crucial Node (并查集)
3、RSS简易阅读器vb.net源代码
4、P2575 高手过招
5、小程序 中 自定义单选框样式


长按二维码向我转账

受苹果公司新规定影响,微信 iOS 版的赞赏功能被关闭,可通过二维码转账支持公众号。

    阅读
    好看
    已推荐到看一看
    你的朋友可以在“发现”-“看一看”看到你认为好看的文章。
    已取消,“好看”想法已同步删除
    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号