在ASP页面上对TABLE进行增、减行的操作

 1<script language="javascript">   
 2function delCol() {   
 3try {   
 4var Elm = event.srcElement;   
 5while(Elm && Elm.tagName != "TR") {   
 6Elm = Elm.parentElement;   
 7}   
 8if(Elm.parentElement.rows.length <= 1) {   
 9alert("无法删除!");   
10return;   
11}   
12Elm.parentElement.deleteRow(Elm.rowIndex);   
13} catch(e) {   
14alert("Err 5001:\r\n" + e);   
15}   
16}   
17function addCol(id) {   
18try {   
19var oTable = document.getElementById(id);   
20if(oTable.tagName != "TABLE")   
21alert("Err 5002");   
22var oList = oTable.children;   
23var oTBODY;   
24for(var i=0;i<oList.length;i++) {   
25if(oList[i].tagName == "TBODY") {   
26oTBODY = oList[i];   
27break;   
28}   
29}   
30var oTR = oTBODY.lastChild;   
31var newTR = oTR.cloneNode(true);   
32addId(newTR);   
33oTBODY.insertAdjacentElement("beforeEnd",newTR);   
34} catch(e) {   
35alert("Err 5002:\r\n" + e);   
36}   
37}   
38function addId(node) {   
39try {   
40if(!node.hasChildNodes()) {   
41var prefix = node.getAttribute("id").split("_")[0];   
42var postfix = node.getAttribute("id").split("_")[1];   
43postfix = parseInt(postfix) + 1;   
44node.setAttribute("id",prefix + "_" + postfix);   
45node.setAttribute("value","");   
46return;   
47}   
48} catch(e) {}   
49try {   
50var oList = node.childNodes;   
51for(var i=0;i<oList.length;i++) {   
52addId(oList[i]);   
53}   
54} catch(e) {   
55alert("Err 5003:\r\n" + e);   
56}   
57}   
58</script>
1<table align="center" border="1" bordercolor="#000000" cellpadding="3" cellspacing="1" class="tableprintable" id="t1" width="100%">
2<tr align="center" class="tdbg0">
3<td class="tdbg0">1</td><td><button class="button" onclick="delCol()">   
4删除</button></td>
5</tr>
6</table>
1<button class="button" onclick="addCol('t1')">添加一行</button>
Published At
Categories with Web编程
comments powered by Disqus