javascript Realizes Adding and Deleting Table Information

  • 2021-11-01 23:28:49
  • OfStack

Getting Started with JavaScript

JavaScript is a lightweight, interpretive Web development language, the language system is not very complex, easy to learn. Since all modern browsers have been embedded in the JavaScript engine, JavaScript source code can be directly interpreted and executed in the browser, users do not have to worry about support issues, this is a small exercise to get started with js

Addition and deletion of table information

Simple DOM operation html tag can be realized, and the code is as follows:

<!DOCTYPE html>
<html lang="en">
 <meta charset="UTF-8">
 <style type="text/css">
   margin: 0;
   padding: 0;
 <script type="text/javascript">

  function delA(){
     // Click the hyperlink to delete the 1 Row 
     // Use this Delete the parent element 
     var tr = this.parentNode.parentNode;

      // Get the name of the person to delete 
     var name=tr.getElementsByTagName("td")[0].innerHTML;
     // Prompt the user whether to delete 
     var flag=confirm(" Delete or not "+name+" ? ");

     // Prevent browser default behaviors, such as popping up new tabs 
     return false;

   // Click the hyperlink to delete 1 Employee information 
   // Get a link 
   var allA=document.getElementsByTagName("a");

   // Binding response function 
   for(var i=0;i<allA.length;i++){

   // Add Employee Feature , Click the button to add information to the table 
   var addEmpButton = document.getElementById("addEmpButton");
    // Get the text content in the input box 
    var empName=document.getElementById("empName").value;
    var email=document.getElementById("email").value;
    var salary=document.getElementById("salary").value;

    // Create 1 A tr
    var tr=document.createElement("tr");
    // Toward tr Add content to 
        "<td><a href='javascript:;'>Delete</a></td>";

     var a= tr.getElementsByTagName("a")[0];
    // Put tr Put on table Medium 
    var employeeTable=document.getElementById("employeeTable");
    // Get tbody
    var tbody=document.getElementsByTagName("tbody")[0];




 <table id="employeeTable">
   <td><a href="">Delete</a></td>
   <td><a href="">Delete</a></td>
   <td><a href="">Delete</a></td>
  <div id="formDiv">
   <h4> Add a new employee </h4>
     <td class="word">name: </td>
     <td class="inp">
      <input type="text" name="empName" id="empName">
     <td class="word">email: </td>
     <td class="inp">
      <input type="text" name="email" id="email">
     <td class="word">salary: </td>
     <td class="inp">
      <input type="text" name="salary" id="salary">
     <td colspan="2" align="center"> <!--colspan And rowspan Property is the number of rows and columns a cell can span -->
      <!--align Property is the text alignment position -->
      <button id="addEmpButton" value="abc">

The comments in the code fragment are very clear, which is suitable for practicing hands.

Related articles: