用javascript刪除當前行,添加行(範例程式碼)_javascript技巧

來源:互聯網
上載者:User

刪除行

複製代碼 代碼如下:

<script>  
  function   del(obj)  
  {  
        obj.parentNode.parentNode.removeNode(true);  
  }    
  </script>  
  <body>  
  <table>  
  <tr>
      <td>儲存格1</td>
      <td><input   type=button   value="delete   this   row"   onclick="del(this)"></td>
   </tr> 
   <tr>
       <td>儲存格2</td>
      <td><input   type=button   value="delete   this   row"   onclick="del(this)"></td>
   </tr>
    <tr>
       <td>儲存格3</td>
      <td><input   type=button   value="delete   this   row"   onclick="del(this)"></td>
   </tr>
  </table>  
  </body>

添加行
複製代碼 代碼如下:

<script>

 function uf_test(){
  //得到table對象
   var tbTable = document.getElementById("tb_test");
   //插入一行
   var trT = tbTable.insertRow();

   //得到上一行的td數
   var nRows = tbTable.rows[0].cells.length;

   //按上一行的td數迴圈進行插入td
   for(var i = 0; i < nRows ; i++){
    //建立td對象
    var tdT = document.createElement("TD");
    //給td對象賦值
    //tdT.innerHTML="sfsdf"; 
    tdT.innerText="sfsdf";
    //把td添加到tr中
    trT.appendChild(tdT);
   }
 }

</script>

<table id="tb_test" border="1">
 <tr>
 <td><input type="text"></td>
 </tr>
</table>

<input type="button" onclick="uf_test();" value="增加一行">

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.