前兩個(gè)刪除問題
為什么其它沒問題,就前兩個(gè)刪除沒反應(yīng)?
<!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();??
? ? ? ? ??
? ? ? }? ? ? ??
? ? ?// 鼠標(biāo)移動(dòng)改變背景,可以通過給每行綁定鼠標(biāo)移上事件和鼠標(biāo)移除事件來改變所在行背景色。
? ? ?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";
}
}
? ? ?}
? ? ? // 編寫一個(gè)函數(shù),供添加按鈕調(diào)用,動(dòng)態(tài)在表格的最后一行添加子節(jié)點(diǎn);
? ? 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>學(xué)號(hào)</th>
<th>姓名</th>
<th>操作</th>
? ?</tr>??
? ?<tr>
<td>xh001</td>
<td>王小明</td>
<td><a href='javascript:;' onclick="deleteRow()">刪除</a></td>? ?<!--在刪除按鈕上添加點(diǎn)擊事件? -->
? ?</tr>
? ?<tr>
<td>xh002</td>
<td>劉小芳</td>
<td><a href='javascript:;' onclick="deleteRow(this)">刪除</a></td>? ?<!--在刪除按鈕上添加點(diǎn)擊事件? -->
? ?</tr>??
? ?</table>
? ?<input type="button" value="添加一行" onclick="addb()"/>? ?<!--在添加按鈕上添加點(diǎn)擊事件? -->
?</body>
</html>
2018-12-20
html中,table標(biāo)簽內(nèi)分為三塊:thead tbody tfoot.tr標(biāo)簽在tbody中。
你的代碼中雖然table標(biāo)簽內(nèi)沒有tbody,但是瀏覽器會(huì)默認(rèn)添加上,所以要?jiǎng)h除的標(biāo)簽tr的父節(jié)點(diǎn)是tbody,而不是table,你從table內(nèi)刪除tr,刪除無效的。
修改如下:
2018-11-21
誰能解釋一下這個(gè)問題 我也想知道
2018-11-19
我也遇到了這個(gè)問題,發(fā)現(xiàn)這個(gè)節(jié)點(diǎn)的父節(jié)點(diǎn)是tbody不是table,不知道是怎么回事