日期:2014-05-16  浏览次数:20412 次

JS 动态添加删除表格------支持火狐

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>动态添加删除表格----支持火狐</title>
</head>
<script type="text/javascript" language="javascript">
function findObj(theObj, theDoc){?
var p, i, foundObj;
??? if(!theDoc) theDoc = document;? if( (p = theObj.indexOf("?")) > 0 && parent.frames.length)? {??? theDoc = parent.frames[theObj.substring(p+1)].document;??? theObj = theObj.substring(0,p);? }? if(!(foundObj = theDoc[theObj]) && theDoc.all) foundObj = theDoc.all[theObj];? for (i=0; !foundObj && i < theDoc.forms.length; i++)???? foundObj = theDoc.forms[i][theObj];? for(i=0; !foundObj && theDoc.layers && i < theDoc.layers.length; i++)???? foundObj = findObj(theObj,theDoc.layers[i].document);? if(!foundObj && document.getElementById) foundObj = document.getElementById(theObj);??? return foundObj;}

? var count=1;
function addTR(){
?var txtTRLastIndex = findObj("txtTRLastIndex",document);
?var imguptb = findObj("imguptb",document);
?
?var imguptb = imguptb.insertRow(imguptb.rows.length);
?//添加名称
?var name = imguptb.insertCell(0);
?name.innerHTML="<input name='name"+count+"' class='name_input' id='name_"+count+"' type='text' maxlength='20' />" ;
?//添加删除按钮
?var delbut = imguptb.insertCell(1);
?delbut.innerHTML="[<a href='#' onclick='deletePic(this);'>删除</a>]";
?count++;
? }
//删除一行
function deletePic(tr_a){
var imguptb = document.getElementById("imguptb");

var trobj = tr_a.parentNode.parentNode;
imguptb.deleteRow(trobj.rowIndex);
}

</script>
?

<body>
<table height="31" border="0" cellpadding="0" cellspacing="2" id="imguptb">
<tr>
<td width="200"><strong>名称</strong></td>
<td width="70"><strong>操作</strong></td>
</tr>
</table>
<table id="table2"><tr><td><input type="button" id ="addbut" name="addbut" onClick="addTR();" value="增加一行数据"/></td></tr></table>
?
</body>
</html>