<div class="codetitle"> <span><a style="CURSOR: pointer" data="49688" class="copybut" id="copybut49688" onclick="doCopy('code49688')"><u>複製程式碼</u></a></span> 代碼如下:</div> <div class="codebody" id="code49688"> <br> <br><br> <br> <br> <br><title>table1.html</title> <br><br><meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <br><meta http-equiv=" description" content="this is my page"> <br><meta http-equiv="content-type" content="text/html; charset=UTF-8"> <br><br><!--< link rel="stylesheet" type="text/css" href="./styles.css">--> <br><script language="javascript" type="text/javascript"> <BR><!- - <BR>function test1(){ <BR>//判斷插入的編號是否已經存在<BR>for(var i=0;i<mytable.rows.length;i ){ <BR>var eachRow = mytable. rows[i]; <BR>if(eachRow.cells[0].innerText == no.value){ <BR>window.alert("編號已經存在!"); <BR>return ; <BR>} <BR>} <BR>//取得表單中的資料<BR>var newTableRow = mytable.insertRow(mytable.rows.length); <BR>newTableRow.insertCell(0).innerText = no.value; .insertCell(1).innerText = name1.value; <BR>newTableRow.insertCell(2).innerText = nickName.value; <br>} <br><BR>function test2(){ <BR>} <BR><BR>function test2(){ <BR>mytable.deleteRow( mytable.rows.length-1); <br>} <br>//--> <BR></script> <br> <br><br> <br><h1>英雄排行榜</h1> <br><table id="mytable" border="1"> <br><tr> <td>排名</td> <td>姓名</td> <td>外號</td> </tr> <br><tr> <td>1</td> <td>宋江</td> <td>及時雨</td> </tr> <br><tr> <td>2</td> <td>盧俊義</td> <td>玉麒麟</td> </tr> <br> </table> <br><h1>請輸入新的好漢</h1> <br>編號<input id="no" type="text" value=""><br> <br>姓名<input id="name1" type="text" value=""><br> <br>外號<input id="nickName" type="text" value=""><br> <br><input id="tianjia" type="button" value="新增" onclick="test1( )"> </div> <input type="button" value="刪除最後一行" onclick="test2()"> <br> <img src="http://files.jb51.net/file_images/article/201401/20140126155644.png?201402615572" alt="table物件中的insertRow與deleteRow使用範例_javascript技巧" >