<!DOCTYPE html><html>?<head>? <title> new document </title> ?? <meta http-equiv="Content-Type" content="text/html; charset=gbk"/> ?? <script type="text/javascript"> ??? function add()??? {??????? var a=prompt("請輸入學號","");??????? var b=prompt("請輸入姓名","");??????? if(a!=""&&b!="")??????? {??????????? var newnode=document.createElement("tr");??????????? newnode.innerHTML='<td>'+a+'</td>'+'<td>'+b+'</td>'+'<td><a href="javascript:;" onclick="del(this)">刪除</a></td>';??????????? document.getElementById("table").appendChild(newnode);??????? }??????? else{confirm("請輸入完整信息(無法創建)");}??? }// 編寫一個函數,供添加按鈕調用,動態在表格的最后一行添加子節點;??? ??? ??? ??? ?? ??? ? ???? function del(tr)???? {??????? document.getElementById("table").removeChild(tr.parentNode.parentNode);???? }// 創建刪除函數??? ?? </script>??? <style type="text/css">??? td:hover{ background-color:#f2f2f2; color:red;}??? th:hover{ background-color:#f2f2f2; color:red;}? </style>?</head> ?<body> ?? ??? <table border="1" width="50%" id="table">?? ??? <tr>?? ??? ?<th>學號</th>?? ??? ?<th>姓名</th>?? ??? ?<th>操作</th>?? ??? </tr> ??? ??? <tr>?? ??? ?<td>xh001</td>?? ??? ?<td>王小明</td>?? ??? ?<td><a href="javascript:;" onclick="del(this)">刪除</a></td>?? <!--在刪除按鈕上添加點擊事件? -->?? ??? </tr>?? ??? <tr>?? ??? ?<td>xh002</td>?? ??? ?<td>劉小芳</td>?? ??? ?<td><a href="javascript:;" onclick="del(this)" >刪除</a></td>?? <!--在刪除按鈕上添加點擊事件? -->?? ??? </tr> ??? ??? </table>?? ??? <input type="button" value="添加一行" onclick="add()" />?? <!--在添加按鈕上添加點擊事件? -->?</body></html>
為什么原有的兩個td不能刪除,新創建的可以刪除呢?
qq_吃藥先森_03169839
2016-04-26 13:56:23