首頁  >  文章  >  web前端  >  js怎麼清空tr

js怎麼清空tr

(*-*)浩
(*-*)浩原創
2019-05-18 17:30:384776瀏覽

js清空tr的實作方法:先取得第一個td的要刪除行的Index;然後進行迴圈運算;最後將tr中的td的資料都刪除一遍即可實作清空tr。

js怎麼清空tr

本篇文章將介紹如何使用js清空tr裡的所有資料。

使用js清空tr裡的數據,首先你要取得第一個td的要刪除行的Index,然後進行循環操作,將tr中的td的數據都刪除一遍,這時tr就被清空了。這是一個思路,下面的程式碼進行這樣寫的,當然,如果你對節點,選擇器理解透徹,還將以將以下程式碼更加的簡化.

請看以下程式碼:

<!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 DeleteSignRow(rowid){
var signFrame = findObj("SignFrame",document);
var signItem = findObj(rowid,document);
alert(rowid);
//获取将要删除的行的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(&#39;确定要清空所有参与人吗?&#39;)){
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";
  

}
}
</script>
</HEAD>
  
<BODY>
<div>
<table width="613" border="0" cellpadding="2" cellspacing="1" id="SignFrame">
       <tr id="trHeader">
        <td width="27" bgcolor="#96E0E2">序号</td>
        <td width="64" bgcolor="#96E0E2">用户姓名</td>
        <td width="98" bgcolor="#96E0E2">电子邮箱</td>
        <td width="92" bgcolor="#96E0E2">固定电话</td>
        <td width="86" bgcolor="#96E0E2">移动手机</td>
        <td width="153" bgcolor="#96E0E2">公司名称</td>
        <td width="57" align="center" bgcolor="#96E0E2"> </td>
       </tr>
    </table>
  </div>
  <div>
    <input type="button" name="Submit" value="添加参与人" onclick="AddSignRow()" /> 
   <input type="button" name="Submit2" value="清空" onclick="ClearAllSign()" />
   <input name=&#39;txtTRLastIndex&#39; type=&#39;hidden&#39; id=&#39;txtTRLastIndex&#39; value="1" />
  </div>
  
</BODY>
</HTML>

以上是js怎麼清空tr的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
上一篇:JSBridge是什麼下一篇:JSBridge是什麼