js實現對table的增加行和刪除行的操作

來源:互聯網
上載者:User

標籤:

<!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=utf-8" />
<title>無標題文檔</title>
</head>

<body>
<script type="text/javascript">
//添加方法
function addtr()
{
    //var trid=0;
    var tab=document.getElementByIdx_x("signFrame");
    //添加行
    var newTR = tab.insertRow(tab.rows.length);
 alert(tab.rows.length);
    //trid++;
   
    //擷取序號=行索引
    var  xuhao=newTR.rowIndex.toString();
 alert(xuhao);
    newTR.id = "tr" + xuhao;
    //添加列:序號
    var newNameTD=newTR.insertCell(0);
    //添加列內容
    newNameTD.innerHTML = xuhao;
    //添加列:日期
    var newNameTD=newTR.insertCell(1);
    //添加列內容
    newNameTD.innerHTML = "<input name=‘time" + xuhao + "‘ id=‘time" + xuhao + " size=‘19‘ onmouseover=‘this.style.backgroundColor=#6298E1;this.style.color=#000000;‘ onmouseout=‘this.style.backgroundColor=#6298E1;this.style.color=#000000;‘ />";

    //添加列:方式
    var newEmailTD=newTR.insertCell(2);
    //添加列內容
    newEmailTD.innerHTML = "<select style=‘width:70px;‘ name=‘way" + xuhao + "‘ id=‘way" + xuhao + "‘><option  value=‘電話‘>電話</option><option value=‘QQ‘>QQ</option>   </select>";

    //添加列:備忘
    var newTelTD=newTR.insertCell(3);
    //添加列內容
    newTelTD.innerHTML = "<input size=‘60‘ name=‘remark" + xuhao + "‘ id=‘remark" + xuhao + "‘ type=‘text‘  onclick=‘showid(this)‘ />";
       
    //添加列:刪除按鈕
    var newDeleteTD=newTR.insertCell(4);
    //添加列內容
    newDeleteTD.innerHTML = "<div align=‘center‘ style=‘width:40px‘><a href=‘javascript:;‘ onclick=\"deltr(‘tr" + xuhao + "‘)\">刪除</a></div>";
       
}
</script>

<script type="text/javascript">
//刪除其中一行
function deltr(trid)
{    //alert(trid);
    var tab=document.getElementByIdx_x("signFrame");
    var row=document.getElementByIdx_x(trid);   
    var index=row.rowIndex;//rowIndex屬性為tr的索引值,從0開始  
    tab.deleteRow(index);  //從table中刪除

    //重新排列序號,如果沒有序號,這一步省略
    var nextid;
    for(i=index;i<tab.rows.length;i++){
        tab.rows[i].cells[0].innerHTML = i.toString();
        nextid=i+1;
        remark=document.getElementByIdx_x("remark"+nextid);
        remark.id="remark";
    }
}

</script>


<script type="text/javascript">
    function showid(txt)
    {alert(txt.id);
        }

</script>
<body>
<input type="button" value="儲存" onclick="addtr()" />


<table border="1px" width="70%" id="signFrame">
    <tr id="trHeader">
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
    </tr>
    <tr id="trHeader">
        <td width="50px">序號</td>
        <td width="170px">時間</td>
        <td width="100px">方式</td>
        <td>備忘</td>
        <td width="80px">操作</td>
    </tr>
    <tr id="trHeader">
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
    </tr>
    <tr id="trHeader">
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
    </tr>
    <tr id="trHeader">
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
    </tr>
    <tr id="trHeader">
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
    </tr>
</table>

 

</body>
</html>

js實現對table的增加行和刪除行的操作

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.