Home > Web Front-end > JS Tutorial > How to delete a table in javascript

How to delete a table in javascript

coldplay.xixi
Release: 2023-01-04 09:35:15
Original
2796 people have browsed it

Javascript implements the method of deleting a table: first create a node; then remove the child node tr under the table and delete the rows of the table. The code is [table.removeChild(this.parentNode.parentNode)].

How to delete a table in javascript

The operating environment of this tutorial: Windows 7 system, JavaScript version 1.8.5, DELL G3 computer.

Javascript method to delete a table:

Rendering:

How to delete a table in javascript

The code is as follows:

<!DOCTYPE html>
<html>
 <head>
 <meta charset="UTF-8">
 <title></title>
 <style type="text/css">
  #box{
  margin:0 auto;
  background:yellow;
  border:4px double #808080;
  width:600px;
  text-align:center;
  }
  #box input{
  width:130px;
  }
  #box table{
  margin:5px 0;
  background:lawngreen;
  }
 </style>
 <script type="text/javascript">
  onload = function(){
  var aInput = document.getElementsByTagName(&#39;input&#39;);
  var bTn = document.getElementById(&#39;btn&#39;);
  var table = document.getElementsByTagName(&#39;table&#39;)[0];
//  
 
  //方法一
//  bTn.onclick = function(){
//   
//   var oTr=document.createElement(&#39;tr&#39;);//创建节点
//   table.appendChild(oTr);//创建table的子节点tr
 
//   var oTd=document.createElement(&#39;td&#39;);//创建节点
//   oTd.innerHTML=aInput[0].value;//给表格赋内容
//   oTr.appendChild(oTd);//创建tr的子节点td
//   
//   var oTd=document.createElement(&#39;td&#39;);//创建节点
//   oTd.innerHTML=aInput[1].value;//给表格赋内容
//   oTr.appendChild(oTd);//创建tr的子节点td
//   
//   var oTd=document.createElement(&#39;td&#39;) ;//创建节点
//   oTd.innerHTML=aInput[2].value;//给表格赋内容
//   oTr.appendChild(oTd);//创建tr的子节点td
 
//   var oTd=document.createElement(&#39;td&#39;) ;//创建节点
//   oTd.innerHTML=&#39;<a href="javascript:;" rel="external nofollow" rel="external nofollow" >删除</a>&#39;;//给表格赋内容
//   oTr.appendChild(oTd);//创建tr的子节点td
 
//   oTd.getElementsByTagName(&#39;a&#39;)[0].onclick=function(){
//   table.removeChild(this.parentNode.parentNode);
//   //移除table下面的子节点tr
//   }//删除表格行
 
  //方法二
  bTn.onclick = function(){
 
   var oTr=document.createElement(&#39;tr&#39;);//创建节点
   table.appendChild(oTr);//创建table的子节点tr
   for(var i = 0; i < aInput.length-1; i ++){
   var oTd=document.createElement(&#39;td&#39;);//创建节点
   oTd.innerHTML=aInput[i].value;//给表格赋内容
   oTr.appendChild(oTd);//创建tr的子节点td
   }
 
   var oTd=document.createElement(&#39;td&#39;) ;//创建节点
   oTd.innerHTML=&#39;<a href="javascript:;" rel="external nofollow" rel="external nofollow" >删除</a>&#39;;//给表格赋内容
   oTr.appendChild(oTd);//创建tr的子节点td
 
   oTd.getElementsByTagName(&#39;a&#39;)[0].onclick=function(){
   table.deleteRow(1);
   }
  }
  }  
 </script>
 </head>
 <body>
 <div id="box">
  ID:<input type="text" name="" id="" value="" />
  姓名:<input type="text" name="" id="" value="" />
  电话:<input type="text" name="" id="" value="" />
  <input type="button" name="btn" id="btn" value="保存" style="width:50px;"/>
  <table border="" cellspacing="" cellpadding="" width="600px" >
  <tr>
  <td>Id</td>
  <td>name</td>
  <td>tal</td>
  <td>操作</td>
  </tr>  
  </table>
 </div>
 
 </body>
</html>
Copy after login

Related free learning recommendations: javascript (Video)

The above is the detailed content of How to delete a table in javascript. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template