Example use of insertRow and deleteRow in the table object

  • 2020-03-30 01:27:36
  • OfStack

 
<!DOCTYPE html> 
<html> 
<head> 
<title>table1.html</title> 

<meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> 
<meta http-equiv="description" content="this is my page"> 
<meta http-equiv="content-type" content="text/html; charset=UTF-8"> 

<!--<link rel="stylesheet" type="text/css" href="./styles.css">--> 
<script language = "javascript" type = "text/javascript"> 
<!-- 
function test1(){ 
//Determines whether the insertion number already exists
for(var i=0;i<mytable.rows.length;i++){ 
var eachRow = mytable.rows[i]; 
if(eachRow.cells[0].innerText == no.value){ 
window.alert(" The number already exists! "); 
return ; 
} 
} 
//Gets the data in the form
var newTableRow = mytable.insertRow(mytable.rows.length); 
newTableRow.insertCell(0).innerText = no.value; 
newTableRow.insertCell(1).innerText = name1.value; 
newTableRow.insertCell(2).innerText = nickName.value; 
} 

function test2(){ 
mytable.deleteRow(mytable.rows.length-1); 
} 
//--> 
</script> 
</head> 

<body> 
<h1> Heroes list </h1> 
<table id = "mytable" border = "1"> 
<tr><td> ranking </td><td> The name </td><td> The nickname </td></tr> 
<tr><td>1</td><td> Sung river </td><td> In Hong Kong </td></tr> 
<tr><td>2</td><td> Jun-yi lu </td><td> Jade kirin </td></tr> 
</table> 
<h1> Please enter a new hero </h1> 
 Serial number <input id = "no" type = "text" value = ""><br/> 
 The name <input id = "name1" type = "text" value = ""><br/> 
 The nickname <input id = "nickName" type = "text" value = ""><br/> 
<input id = "tianjia" type = "button" value = " add " onclick = "test1()"> 
<input type = "button" value = " Delete the last line " onclick = "test2()"/> 
</body> 
</html> 

 

Related articles: