前兩個刪除問題
為什么其它沒問題,就前兩個刪除沒反應?
<!DOCTYPE html>
<html>
?<head>
? <title> new document </title>??
? <meta http-equiv="Content-Type" content="text/html; charset=gbk"/>? ?
? <script type="text/javascript">?
? ? ? ??
? ? ? window.onload = function(){
? ? ? ? Hl();??
? ? ? ? ??
? ? ? }? ? ? ??
? ? ?// 鼠標移動改變背景,可以通過給每行綁定鼠標移上事件和鼠標移除事件來改變所在行背景色。
? ? ?function Hl(){
? ? ?var tr = document.getElementsByTagName("tr");
? ? ?for(var i=1;i<tr.length;i++){
? ? ?tr[i].onmouseover = function(){
? ? ? ? this.style.backgroundColor="#f2f2f2";
}
tr[i].onmouseout = function(){
? ? ?this.style.backgroundColor="#fff";
}
}
? ? ?}
? ? ? // 編寫一個函數(shù),供添加按鈕調用,動態(tài)在表格的最后一行添加子節(jié)點;
? ? function addb(){
? ? ? ? var table = document.getElementById("table");
? ? ? ? var new_tr = document.createElement("tr");
? ? ? ? table.appendChild(new_tr);
? ? ? ? for(var i=0;i<2;i++){
? ? ? ? ? ? var new_td = document.createElement("td");
? ? ? ? ? ? new_tr.appendChild(new_td);
? ? ? ? ? ? new_td.innerHTML = "<input type='text'/>";
? ? ? ? }
? ? ? ? var new_td = document.createElement("td");
? ? ? ? ? ? new_tr.appendChild(new_td);
? ? ? ? ? ? new_td.innerHTML = "<a href='javascript:;' onclick='deleteRow(this)'>刪除</a>";
? ? Hl();?
? ? }
? ?
? ? ?
? ? ?// 創(chuàng)建刪除函數(shù)
? ? ?function deleteRow(obj){
? ? ? ? ?var table = document.getElementById("table");
? ? ? ? table.removeChild(obj.parentNode.parentNode);
? ? ? ? Hl();?
? ? ?}
? ? ?
? </script>?
?</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="deleteRow()">刪除</a></td>? ?<!--在刪除按鈕上添加點擊事件? -->
? ?</tr>
? ?<tr>
<td>xh002</td>
<td>劉小芳</td>
<td><a href='javascript:;' onclick="deleteRow(this)">刪除</a></td>? ?<!--在刪除按鈕上添加點擊事件? -->
? ?</tr>??
? ?</table>
? ?<input type="button" value="添加一行" onclick="addb()"/>? ?<!--在添加按鈕上添加點擊事件? -->
?</body>
</html>
2018-12-20
html中,table標簽內分為三塊:thead tbody tfoot.tr標簽在tbody中。
你的代碼中雖然table標簽內沒有tbody,但是瀏覽器會默認添加上,所以要刪除的標簽tr的父節(jié)點是tbody,而不是table,你從table內刪除tr,刪除無效的。
修改如下:
2018-11-21
誰能解釋一下這個問題 我也想知道
2018-11-19
我也遇到了這個問題,發(fā)現(xiàn)這個節(jié)點的父節(jié)點是tbody不是table,不知道是怎么回事