This article describes how js implements dynamic addition, deletion and update of tables. Share it for your reference. The specific implementation method is as follows:
The code copy is as follows:
<!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>Table Operation</title>
<style type="text/css">
body {
font-size: 13px;
line-height: 25px;
}
table {
border-top: 1px solid #333;
border-bottom: 1px solid #333;
width: 300px;
}
td {
border-right: 1px solid #333;
border-bottom: 1px solid #333;
}
.title {
text-align: center;
font-weight: bold;
background: #ccc;
}
.center {
text-align: center;
}
#displayInfo {
color: red;
}
</style>
<script type="text/javascript">
function addRow() { //Add one line
var tableObj = document.getElementById('myTable');
var rowNums = tableObj.rows.length;
var newRow = tableObj.insertRow(rowNums);
var col1 = newRow.insertCell(0);
col1.innerHTML = "Happiness falls from the sky";
var col2 = newRow.insertCell(1);
col2.innerHTML = "$18.5";
col2.align = "center";
var divInfo = document.getElementById('displayInfo');
divInfo.innerHTML = "Add product successfully";
}
function delRow() { //Delete the second line
document.getElementById('myTable').deleteRow(1);
var divInfo = document.getElementById('displayInfo');
divInfo.innerHTML = "Delete the product successfully";
}
function updateRow() { //Update line information
var uRow = document.getElementById('myTable').rows[0];
uRow.className = "title";
}
</script>
</head>
<body>
<table cellpadding="0" cellpacing="0" id="mytable">
<tr id="row1">
<td>Book Title</td>
<td>Price</td>
</tr>
<tr id="row2">
<td>The room with views</td>
<td>$30.00</td>
</tr>
<tr id="row3">
<td>60 Moments</td>
<td>$32.00</td>
</tr>
</table>
<input name="b1" type="button" value="add one line" onclick="javascript:addRow();"/><br />
<input name="b2" type="button" value="Delete the second line" onclick="javascript:delRow();"/><br />
<input name="b3" type="button" value="Modify title" onclick="javascript:updateRow();"/><br />
<div id="displayInfo"></div>
</body>
</html>
I hope this article will be helpful to everyone's JavaScript programming.