TinyMind
.net.cn
首页
资讯
文章
注册
登录
平常用到的一些js代码
一望无际的南
关注文章
TinyMind专栏频道
/**/
/*
标准程序块
Author:KingAstar
Date:2005-11-10
*/
/**/
/*
*****************************************************
Desp:调用远程数据
Date:2005-11-9
Author:Kingastar
Params:改变的本地Div或者其它元素的ID,远程调用的URL,处理函数
如果处理函数为空,则自动将返回的HTML代码设置为元素内部的HTML
******************************************************
*/
function
LoadDataInContainer(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
=
new
XMLHttpRequest();
req.onreadystatechange
=
processReqChange;
req.open(
"
GET
"
,url,
true
);
req.send(
null
);
//
branchforIE/WindowsActiveXversion
}
else
if
(window.ActiveXObject)
{
isIE
=
true
;
req
=
new
ActiveXObject(
"
Microsoft.XMLHTTP
"
);
if
(req)
{
req.onreadystatechange
=
processReqChange;
req.open(
"
GET
"
,url,
true
);
req.send();
}
}
}
//
数据返回后处理
function
processReqChange()
{
//
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);
}
}
}
//
显示进程正在进行
function
ShowProgress(status)
{
var
progressDiv
=
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的题头也变
*****************************************************
*/
function
AttachTableOnchangeEvent(table,NewColor,includingTitle)
{
this
.newColor
=
NewColor;
this
.includingtitle
=
includingTitle;
table.attachEvent(
"
onmousedown
"
,OnClickChangeTdBackColor);
}
function
OnClickChangeTdBackColor()
{
var
srcElement
=
event.srcElement;
//
varEverselected="whitesmoke";
var
TabElement
=
event.srcElement;
var
oldcolor;
while
(srcElement.tagName.toLowerCase()
!=
"
tr
"
)
{
srcElement
=
srcElement.parentElement;
}
while
(TabElement.tagName.toLowerCase()
!=
"
table
"
)
{
TabElement
=
TabElement.parentElement;
}
//
alert(srcElement.tagName);
for
(
var
i
=
0
;i
<
TabElement.rows.length;i
++
)
{
if
(TabElement.rows[i].style.background
!=
this
.newColor)
{
oldcolor
=
TabElement.rows[i].style.background;
break
;
}
}
for
(
var
i
=
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参数
*****************************************************
*/
function
getVirtualPath(reUrl)
{
var
ss,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]
+
"
/
"
;
}
}
return
ss
+
reUrl;
}
/**/
/*
*****************************************************
Desp:Animplementationofthedebuglog.
Date:2005-11-15
Author:googleinc
*****************************************************
*/
var
logging__
=
false
;
function
Log()
{}
;
Log.lines
=
[];
Log.write
=
function
(s)
{
if
(logging__)
{
this
.lines.push(xmlEscapeText(s));
this
.show();
}
}
;
//
WritesthegivenXMLwitheverytagonanewline.
Log.writeXML
=
function
(xml)
{
if
(logging__)
{
var
s0
=
xml.replace(
/</
g,'\n
<
');
var
s1
=
xmlEscapeText(s0);
var
s2
=
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__)
{
var
l
=
this
.div();
l.innerHTML
=
'';
this
.lines
=
[];
}
}
Log.show
=
function
()
{
var
l
=
this
.div();
l.innerHTML
+=
this
.lines.join('
<
br
/>
')
+
'
<
br
/>
';
this
.lines
=
[];
l.scrollTop
=
l.scrollHeight;
}
Log.div
=
function
()
{
var
l
=
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);
}
return
l;
}
/**/
/*
Desp:产生下拉列过滤选择项;Attachthefiltertoatable.
Date:2005-11-9
Author:?
Params:
filterRowspecifiestherownumberatwhichthefiltershouldbeinserted.
Modify:
主要比较的是内容,所有把innerHTML换成了innerText
另外就是在特定的列创建
2.0
*/
function
attachFilter2(table,filterRow)
{
table.filterRow
=
filterRow;
//
Checkifthetablehasanyrows.Ifnot,donothing
if
(table.rows.length
>
0
)
{
//
Insertthefilterrowandaddcellswhithdrowdowns.
var
filterRow
=
table.insertRow(table.filterRow);
table.fillFilters
=
fillFilters;
table.inFilter
=
inFilter;
table.buildFilter
=
buildFilter;
table.showAll
=
showAll;
table.detachFilter
=
detachFilter;
table.filterElements
=
new
Array();
table.filterElements[
0
]
=
2
;
var
colLen
=
table.rows[
0
].cells.length;
alert(colLen);
for
(
var
i
=
0
;i
<
table.rows[table.filterRow
+
1
].cells.length;i
++
)
{
var
c
=
document.createElement(
"
TH
"
);
table.rows[table.filterRow].appendChild(c);
var
opt
=
document.createElement(
"
select
"
);
opt.onchange
=
filter;
if
(
!
table.inFilter(colLen
-
i))
{
c.appendChild(opt);
}
else
{
c.innerText
=
""
;
}
}
//
Setthefunctions
//
Fillthefilters
table.fillFilters();
table.filterEnabled
=
true
;
}
}
function
detachFilter()
{
if
(
this
.filterEnabled)
{
//
Removethefilter
this
.showAll();
this
.deleteRow(
this
.filterRow);
this
.filterEnabled
=
false
;
}
}
//
Checksifacolumnisfiltered
function
inFilter(col)
{
for
(
var
i
=
0
;i
<
this
.filterElements.length;i
++
)
{
//
if(this.filterElements[i].index==col)
if
(
this
.filterElements[i]
==
col)
return
true
;
}
return
false
;
}
//
Fillsthefiltersforcolumnswhicharenotfiiltered
function
fillFilters()
{
for
(
var
col
=
0
;col
<
this
.rows[
this
.filterRow].cells.length;col
++
)
{
if
(
!
this
.inFilter(col))
{
this
.buildFilter(col,
"
(all)
"
);
}
}
}
//
Fillsthecolumnsdropdownbox.
//
setValueisthevaluewhichthedropdownboxshouldhaveonefilled.
//
Ifthevalueisnotsuplied,thefirstitemisselected
function
buildFilter(col,setValue)
{
//
Getareferencetothedropdownbox.
var
opt
=
this
.rows[
this
.filterRow].cells[col].firstChild;
//
removeallexistingitems
while
(opt.length
>
0
)
opt.remove(
0
);
var
values
=
new
Array();
//
putallrelevantstringsinthevaluesarray.
for
(
var
i
=
this
.filterRow
+
1
;i
<
this
.rows.length;i
++
)
{
var
row
=
this
.rows[i];
if
(row.style.display
!=
"
none
"
&&
row.className
!=
"
noFilter
"
)
{
values.push(row.cells[col].innerText.toLowerCase());
}
}
values.sort();
//
addeachuniquestringtothedopdownbox
var
value;
for
(
var
i
=
0
;i
<
values.length;i
++
)
{
if
(values[i].toLowerCase()
!=
value)
{
value
=
values[i].toLowerCase();
opt.options.add(
new
Option(values[i],value));
}
}
opt.options.add(
new
Option(
"
(all)
"
,
"
(all)
"
),
0
);
if
(setValue
!=
undefined)
opt.value
=
setValue;
else
opt.options[
0
].selected
=
true
;
}
//
Thisfunctioniscalledwhenadropdownboxchanges
function
filter()
{
var
table
=
this
;
//
'this'isareferencetothedropdownboxwhichchanged
while
(table.tagName.toUpperCase()
!=
"
TABLE
"
)
table
=
table.parentNode;
var
filterIndex
=
this
.parentNode.cellIndex;
//
Thecolumnnumberofthecolumnwhichshouldbefiltered
var
filterText
=
table.rows[table.filterRow].cells[filterIndex].firstChild.value;
//
Firstcheckifthecolumnisallreadyinthefilter.
var
bFound
=
false
;
for
(
var
i
=
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
var
obj
=
new
Object();
obj.filter
=
filterText;
obj.index
=
filterIndex;
table.filterElements.push(obj);
}
//
firstsetallrowstobedisplayed
table.showAll();
//
thefilteroutherightrows.
for
(
var
i
=
0
;i
<
table.filterElements.length;i
++
)
{
//
Firstfillthedropdownboxforthiscolumn
table.buildFilter(table.filterElements[i].index,table.filterElements[i].filter);
//
Applythefilter
for
(
var
j
=
table.filterRow
+
1
;j
<
table.rows.length;j
++
)
{
var
row
=
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();
}
function
showAll()
{
for
(
var
i
=
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字,当前共
字
发送中
网络异常,请稍后重试
知道了
微信扫一扫
关注该公众号
0
│
收藏
│
举报