JavaScript introduction
JavaScript It's a lightweight , Interpretive Web development language , The language system is not very complicated , Easy to learn . Because all modern browsers are embedded JavaScript engine ,JavaScript The source code can be interpreted and executed directly in the browser , Users don't have to worry about support , This is a problem js How to get started
The addition and deletion of form information
ordinary DOM operation html Tag , The code is as follows :
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document
</title> <style type="text/css"> *{ margin: 0; padding: 0; } </style> <script
type="text/javascript"> function delA(){ // Click on the hyperlink to delete the line // use this, Delete parent element var tr =
this.parentNode.parentNode; // Gets the name of the person to be deleted var name=tr.getElementsByTagName("td")[
0].innerHTML; // Prompt user to delete var flag=confirm(" Delete "+name+"?"); if(flag){ tr.
parentNode.removeChild(tr); } // Prevent browser default behavior , For example, pop up a new tab return false; } window.
onload=function(){ // Click the hyperlink to delete an employee's information // Get a link var allA=document.getElementsByTagName
("a"); // Binding response function for(var i=0;i<allA.length;i++){ allA[i].onclick=delA; }
// Add employee function , Click the button to add the information to the form var addEmpButton = document.getElementById("addEmpButton"
); addEmpButton.onclick=function(){ // Gets 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 a tr var tr=
document.createElement("tr"); // towards tr Add content to tr.innerHTML="<td>"+empName+"</td>"+
"<td>"+email+"</td>"+ "<td>"+salary+"</td>"+ "<td><a
href='javascript:;'>Delete</a></td>"; var a= tr.getElementsByTagName("a")[0]; a.
onclick=delA; // hold tr Put in table in var employeeTable=document.getElementById(
"employeeTable"); // obtain tbody var tbody=document.getElementsByTagName("tbody")[0];
tbody.appendChild(tr); } } </script> </head> <body> <table id="employeeTable">
<tr> <th>Name</th> <th>Email</th> <th>Salary</th> <th> </th> </tr> <tr> <td
>Tom</td> <td>[email protected]</td> <td>5000</td> <td><a href="">Delete</a></td> </tr
> <tr> <td>Jerry</td> <td>[email protected]</td> <td>8000</td> <td><a href="">
Delete</a></td> </tr> <tr> <td>Bob</td> <td>[email protected]</td> <td>10000</td> <td>
<a href="">Delete</a></td> </tr> <div id="formDiv"> <h4> Add new employee </h4> <table> <tr>
<td class="word">name: </td> <td class="inp"> <input type="text" name="empName"
id="empName"> </td> </tr> <tr> <td class="word">email: </td> <td class="inp"> <
input type="text" name="email" id="email"> </td> </tr> <tr> <td class="word">
salary:</td> <td class="inp"> <input type="text" name="salary" id="salary"> </td
> </tr> <tr> <td colspan="2" align="center">
<!--colspan and rowspan Properties are the number of rows and columns a cell can span --> <!--align Property is the text alignment position --> <button id="
addEmpButton" value="abc"> Submit </button> </td> </tr> </table> </div> </table>
</body> </html>
The comments in the code snippet are very clear , Suitable for hand training .
Technology