<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>无标题文档</title>
</head>
<SCRIPT LANGUAGE=javascript>
function delCol() {
try {
var Elm = event.srcElement;
while(Elm && Elm.tagName != "TR") {
Elm = Elm.parentElement;
}
if(Elm.parentElement.rows.length <= 1) {
alert("无法删除!");
return;
}
Elm.parentElement.deleteRow(Elm.rowIndex);
} catch(e) {
alert("Err 5001:/r/n" + e);
}
}
function addCol(id) {
try {
var oTable = document.getElementById(id);
if(oTable.tagName != "TABLE")
alert("Err 5002");
var oList = oTable.children;
var oTBODY;
for(var i=0;i<oList.length;i++) {
if(oList[i].tagName == "TBODY") {
oTBODY = oList[i];
break;
}
}
var oTR = oTBODY.lastChild;
var newTR = oTR.cloneNode(true);
addId(newTR);
oTBODY.insertAdjacentElement("beforeEnd",newTR);
} catch(e) {
alert("Err 5002:/r/n" + e);
}
}
function addId(node) {
try {
if(!node.hasChildNodes()) {
var prefix = node.getAttribute("id").split("_")[0];
var postfix = node.getAttribute("id").split("_")[1];
postfix = parseInt(postfix) + 1;
node.setAttribute("id",prefix + "_" + postfix);
node.setAttribute("value","");
return;
}
} catch(e) {}
try {
var oList = node.childNodes;
for(var i=0;i<oList.length;i++) {
addId(oList[i]);
}
} catch(e) {
alert("Err 5003:/r/n" + e);
}
}
</SCRIPT>
<body>
<table width="100%" border="1" align="center" cellpadding="3" cellspacing="1" bordercolor="#000000" class="tableprintable" id="t1">
<tr align="center" class="tdbg0">
<td class="tdbg0">1</td><td><button class="button" onClick="delCol()">
删除</button></td>
</tr>
</table>
<button class="button" onClick="addCol('t1')">添加一行</button>
</td>
</tr>
</table>
</body>
</html>
javascript添加删除行
最新推荐文章于 2023-09-24 15:39:42 发布