為什么刪除沒用
<!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(){ ?????//?鼠標(biāo)移動改變背景,可以通過給每行綁定鼠標(biāo)移上事件和鼠標(biāo)移除事件來改變所在行背景色。 ?????????var?trlist=document.getElementsByTagName("tr"); ?????????//document.write(trlist.length); ?????????for(i=0;i<trlist.length;i++){ ????????????trlist[i].onmouseover=function(){ ????????????????this.style.backgroundColor="#f2f2f2"; ????????????} ????????????trlist[i].onmouseout=function(){ ????????????????this.removeAttribute("style"); ????????????} ?????????} ?????} ????? ??????//?編寫一個(gè)函數(shù),供添加按鈕調(diào)用,動態(tài)在表格的最后一行添加子節(jié)點(diǎn); ?????function?addRow(){ ????????var?newrow=document.createElement("tr"); ????????var?c1=document.createElement("td"); ????????var?text1=document.createTextNode("xh00x"); ????????var?c2=document.createElement("td"); ????????var?c3=document.createElement("td"); ????????var?a=document.createElement("a"); ????????document.getElementById("table").appendChild(newrow); ????????newrow.appendChild(c1); ????????newrow.appendChild(c2); ????????newrow.appendChild(c3); ????????c1.appendChild(text1); ????????c3.appendChild(a); ????????c2.innerHTML="小明"; ????????a.href="javascript:removeRow(this);"; ????????a.innerHTML="刪除"; ?????}???? ???????? ?????//?創(chuàng)建刪除函數(shù) ?????function?removeRow(obj){ ????????var?tr=obj.parentNode.parentNode; ????????tr.parentNode.removeChild(tr); ?????} ??</script>? ?</head>? ?<body>? ???????<table?border="1"?width="50%"?id="table"> ???????<tr> ????????<th>學(xué)號</th> ????????<th>姓名</th> ????????<th>操作</th> ???????</tr>?? ???????<tr> ????????<td>xh001</td> ????????<td>王小明</td> ????????<td><a?href="javascript:removeRow(this);"?onclick="removeRow(this)">刪除</a></td>???<!--在刪除按鈕上添加點(diǎn)擊事件??--> ???????</tr> ???????<tr> ????????<td>xh002</td> ????????<td>劉小芳</td> ????????<td><a?href="javascript:removeRow(this);"?onclick="removeRow(this)">刪除</a></td>???<!--在刪除按鈕上添加點(diǎn)擊事件??--> ???????</tr>?? ???????</table> ???????<input?type="button"?value="添加一行"?onclick="addRow()"?/>???<!--在添加按鈕上添加點(diǎn)擊事件??--> ?</body> </html>
a標(biāo)簽中<a href="javascript:;"onclick="removeRow(this)">用onclick調(diào)用刪除函數(shù)是可以刪除行的。
<a href="javascript:removeRow(this);" >這種方式為什么不行?
2018-12-20
<a?
href=
"javascript:removeRow(this);"
?onclick=
"removeRow(this)"
>刪除</a>
href里面不用寫東西,多此一舉;需要添加額外事件觸發(fā),多個(gè)removeRow,它會執(zhí)行第一個(gè);但是href是頁面跳轉(zhuǎn)用的,它的this值是window,不是當(dāng)前元素,就導(dǎo)致找不到需要刪除的內(nèi)容。
2018-12-20
2018-12-20
<script type="text/javascript">?
? ? window.onload = function(){
? ? ? ? var tr=document.getElementsByTagName("tr");
? ? ? ? for(var i= 0;i<tr.length;i++){
? ? ? ? ? ? bgcChange(tr[i]);
? ? ? ? }?
}
function bgcChange(obj)
? ? ?{
? ? ? ? obj.onmouseover=function(){
? ? ? ? ? ? obj.style.backgroundColor="yellow";
? ? ? ? }
? ? ? ? obj.onmouseout=function(){
? ? ? ? ? ? obj.style.backgroundColor="red";
? ? ? ? }
? ? ?}?
? ? ? // 編寫一個(gè)函數(shù),供添加按鈕調(diào)用,動態(tài)在表格的最后一行添加子節(jié)點(diǎn);
? ? ?var num = 2;
? ? ?function addRow(){
? ? ? ? num++;
? ? ? ? var table=document.getElementById("table");
? ? ? ? var tr=document.createElement("tr");
? ? ? ? var xh=document.createElement("td");
? ? ? ? var xm=document.createElement("td");
? ? ? ? var del=document.createElement("td");
? ? ? ? xh.innerHTML = "xh"+num;
? ? ? ? xm.innerHTML = "第"+num+"個(gè)學(xué)生";
? ? ? ? del.innerHTML = "<a href='javascript:;' onclick='removeRow(this)' >刪除</a>";
? ? ? ? table.appendChild(tr);
? ? ? ? tr.appendChild(xh);
? ? ? ? tr.appendChild(xm);
? ? ? ? tr.appendChild(del);
? ? ? ? bgcChange(tr);
? ? ?}
? ? ?// 創(chuàng)建刪除函數(shù)
? ? ?function removeRow(obj){
? ? ? ? var tr=obj.parentNode.parentNode;
? ? ? ? tr.parentNode.removeChild(tr);
? ? ?}
? </script>
這是我參考別人代碼改寫的,你研究一下
2018-12-20
哦哦 ,再問一個(gè)問題。上述代碼修改以下
前面能成功我能理解,但是后面的給a標(biāo)簽設(shè)置onclick屬性,為什么刪除函數(shù)沒有成功執(zhí)行?