<!DOCTYPE?html>
<html>
?<head>
??<title>?new?document?</title>??
??<meta?http-equiv="Content-Type"?content="text/html;?charset=gbk"/>???
??<style>
??????table{
??????????background:#edf1f2;
??????}
????
??</style>
??<script?type="text/javascript">?
??
??????window.onload?=?function?(){
???????? ?//?鼠標(biāo)移動改變背景,可以通過給每行綁定鼠標(biāo)移上事件和鼠標(biāo)移除事件來改變所在行背景色。
??????var?tr=document.getElementsByTagName('tr')?;
??????for(var?i=0;i<tr.length;i++)
{tr[i].onmousemove=function?(){
???this.style.background='#ff0';
???this.onmouseout=function(){
????????this.style.background='#edf1f2';
????}
}
}
?????????
?????
??????}?????
?????
??????//?編寫一個函數(shù),供添加按鈕調(diào)用,動態(tài)在表格的最后一行添加子節(jié)點(diǎn);
????function?btn(){
????????var?table=document.getElementById('table');
????????var?r=document.getElementsByTagName('tr').length;
????????var?tr1=document.createElement('tr');
???????for(var?i=0;i<3;i++)
?????????{
?????????????var?td=document.createElement('td');
????????????if(i==0)?{td.innerHTML='xh00'+r;}
????????????else?if(i==1){td.innerHTML='name';}
????????????else{
???????????????/*?var?a=document.createElement('a');
????????????????a.innerHTML='刪除';
????????????????a.href="javascript:;";
????????????????a.onclick='remove()'
????????????????td.appendChild(a)}*/
????????????????td.innerHTML="<a?href='javascript:;'?onclick='remove()'?>?刪除</a>";}
?????????tr1.appendChild(td);
?????????}
?????????
?????table.appendChild(tr1);???
?????
?????var?tr=document.getElementsByTagName('tr')?;
??????for(var?i=0;i<tr.length;i++)
{tr[i].onmousemove=function?(){
???this.style.background='#ff0';
???this.onmouseout=function(){
????????this.style.background='#edf1f2';
????}
}
}
?????}
??????
?????//?創(chuàng)建刪除函數(shù)
?????function?remove(){
?????????var?e=event||window.event;
?????????e.target.parentNode.parentNode.parentNode.removeChild(e.target.parentNode.parentNode);
?????????
?????????
?????}
??</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:;"?onclick='remove()'?>刪除</a></td>???<!--在刪除按鈕上添加點(diǎn)擊事件??-->
???</tr>
???<tr>
<td>xh002</td>
<td>劉小芳</td>
<td><a?href="javascript:;"?onclick='remove()'?>刪除</a></td>???<!--在刪除按鈕上添加點(diǎn)擊事件??-->
???</tr>??
???</table>
???<input?type="button"?value="添加一行"?onclick='btn()'?/>???<!--在添加按鈕上添加點(diǎn)擊事件??-->
?</body>
</html>
2017-11-20
終于自己解決了!
1新添加的行無法移入移出變色,解決方法將移入移出變色的函數(shù)復(fù)制到添加行函數(shù)的最后(或者是將移入移出函數(shù)調(diào)用一下);
2新添加的行無法調(diào)用刪除函數(shù),
//原來的代碼
var?a=document.createElement('a');
????????????????a.innerHTML='刪除';
????????????????a.href="javascript:;";
????????????????a.onclick='remove()';
????????????????td.appendChild(a);
//更改后的代碼
td.innerHTML="<a?href='javascript:;'?onclick='remove()'?>?刪除</a>";
這個雖然解決了,但是我不太懂為什么上邊的代碼a.onclick='remove';無法執(zhí)行,新添加的行在開發(fā)模式里沒有顯示onclick的屬性。有大神能來解答一下嗎
2017-11-20
現(xiàn)在就是新添加的行無法調(diào)用刪除函數(shù)了
2017-11-20
你這函數(shù)有問題吧,onmouseover和onmouseout是分開的