网站地图    收藏   

主页 > 前端 > javascript >

js增加删除表table行tr二种实例

来源:自学PHP网    时间:2014-09-19 14:47 作者: 阅读:

[导读] 收款了两款不错的js增加删除表table行tr二种实例,有需要了解的朋友可参考,希望此方法对各位所有帮助。...

例1

 代码如下 复制代码

<html>
<head>
<title>js insert delete talbe row</title> 
<script type="text/javascript">
function inRow()
{
 var table = document.getElementById("tid"); 
 var nextIndex = table.rows.length
 var nextRow = table.insertRow(nextIndex); 
 nextRow.insertCell(0).innerHTML = "A"+nextIndex;
 nextRow.insertCell(1).innerHTML = "B"+nextIndex;
 nextRow.insertCell(2).innerHTML = "C"+nextIndex;
 nextRow.insertCell(3).innerHTML = '<a href="javascript:;" onclick="return del(this)">删除</a>';
 return false;
}
function del(obj)
{
 var table = obj.parentNode.parentNode.parentNode;
 table.deleteRow(obj.parentNode.parentNode.rowIndex); 
}
</script>
</head> 
<body>
<input type="button" name="insert" value="增加" onclick="return inRow()" />
<table border="1" width="300" name="tt" id="tid">
 <tr>
 <td>A0</td>
 <td>B0</td>
 <td>C0</td>
 <td><a href="javascript:;" onclick="return del(this)">删除</a></td> 
</tr>
<tr>
 <td>A1</td>
 <td>B1</td>
 <td>C1</td>
 <td><a href="javascript:;" onclick="return del(this)">删除</a></td> 
</tr>
<tr>
 <td>A2</td>
 <td>B2</td>
 <td>C2</td>
 <td><a href="javascript:;" onclick="return del(this)">删除</a></td> 
</tr>
<tr>
 <td>A3</td>
 <td>B3</td>
 <td>C3</td>
 <td><a href="javascript:;" onclick="return del(this)">删除</a></td> 
</tr> 
</table>
</body>
</html>

例2

 代码如下 复制代码

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE>New Document</TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">
<script language="javascript">// Example: obj = findObj("image1");
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;
}
//添加一个事件填写行
function AddSignRow(){ //读取最后一行的行号,存放在txtTRLastIndex文本框中
var txtTRLastIndex = findObj("txtTRLastIndex",document);
var rowID = parseInt(txtTRLastIndex.value);
var signFrame = findObj("SignFrame",document);
//添加行
var newTR = signFrame.insertRow(signFrame.rows.length);
newTR.id = "SignItem" + rowID;
//添加列:序号
var newNameTD=newTR.insertCell(0);
//添加列内容
newNameTD.innerHTML = newTR.rowIndex.toString();
//添加列:事件ID
var newNameTD=newTR.insertCell(1);
//添加列内容
newNameTD.innerHTML = "<input name='txtName" + rowID + "' id='paraid0" + rowID + "' type='text' size='12' />";
//添加列:事件内容
var newEmailTD=newTR.insertCell(2);
//添加列内容
newEmailTD.innerHTML = "<input name='txtEMail" + rowID + "' id='paraco0" + rowID + "' type='text' size='50' />";
//添加列:删除按钮
var newDeleteTD=newTR.insertCell(3);
//添加列内容
newDeleteTD.innerHTML = "<div align='center' style='width:40px'><a href='javascript:;' onclick="DeleteSignRow('SignItem" + rowID + "')">删除</a></div>";
//将行号推进下一行
txtTRLastIndex.value = (rowID + 1).toString() ;
}
//删除指定行
function DeleteSignRow(rowid){
var signFrame = findObj("SignFrame",document);
var signItem = findObj(rowid,document);
//获取将要删除的行的Index
var rowIndex = signItem.rowIndex;
//删除指定Index的行
signFrame.deleteRow(rowIndex);
//重新排列序号,如果没有序号,这一步省略
for(i=rowIndex;i<signFrame.rows.length;i++){
signFrame.rows[i].cells[0].innerHTML = i.toString();
}
}
//清空列表
function ClearAllSign(){
if(confirm('确定要清空所有参与人吗?')){
var signFrame = findObj("SignFrame",document);
var rowscount = signFrame.rows.length;
//循环删除行,从最后一行往前删除
for(i=rowscount - 1;i > 0; i--){
   signFrame.deleteRow(i);
}
//重置最后行号为1
var txtTRLastIndex = findObj("txtTRLastIndex",document);
txtTRLastIndex.value = "1";
//预添加一行
AddSignRow();
}
}
</script>
</HEAD>
<BODY>
<div>
  <table width="613" border="0" cellpadding="2" cellspacing="1" id="SignFrame">
    <tr id="trHeader">
      <td width="40" bgcolor="#96E0E2">序号</td>
      <td width="50" bgcolor="#96E0E2">事件ID</td>
      <td width="120" bgcolor="#96E0E2">事件内容</td>
      <td width="50" align="center" bgcolor="#96E0E2">&nbsp;</td>
    </tr>
  </table>
</div>
<div>
  <input type="button" name="Submit" value="添加事件" onclick="AddSignRow()" />
  <input type="button" name="Submit2" value="清空事件" onclick="ClearAllSign()" />
  <input name='txtTRLastIndex' type='hidden' id='txtTRLastIndex' value="1" />
</div>
</BODY>
</HTML>

自学PHP网专注网站建设学习,PHP程序学习,平面设计学习,以及操作系统学习

京ICP备14009008号-1@版权所有www.zixuephp.com

网站声明:本站所有视频,教程都由网友上传,站长收集和分享给大家学习使用,如由牵扯版权问题请联系站长邮箱904561283@qq.com

添加评论