刪除一行后,如何讓新添加行的學號遞增且不重復呢
<!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?()?{ ????????changeBg(); ????}; ????function?changeBg()?{ ????????//?鼠標移動改變背景,可以通過給每行綁定鼠標移上事件和鼠標移除事件來改變所在行背景色。 ????????var?trs?=?document.getElementsByTagName("tr"), ????????????body?=?document.getElementsByTagName("body")[0]; ????????for?(var?i?=?0;?i?<?trs.length;?i++)?{ ????????????trs[i].onmouseover?=?function?()?{ ????????????????for?(var?j?=?0;?j?<?trs.length?&&?j?!=?i;?j++)?{ ????????????????????trs[j].style.background?=?"#fff"; ????????????????} ????????????????this.style.background?=?"#ccc"; ????????????}; ????????????trs[i].onmouseout?=?function?()?{ ????????????????this.style.background?=?"#fff"; ????????????} ????????} ????} ??????//?編寫一個函數(shù),供添加按鈕調(diào)用,動態(tài)在表格的最后一行添加子節(jié)點; ????function?addTable(){ ????????var?table?=?document.getElementById("table"), ????????????newNode?=?document.createElement("tr"), ????????????index?=?table.childNodes.length+1; ????????newNode.innerHTML?=?"<td>xh00"+?index?+"</td><td></td><td><a?href='javascript:;'??onclick='removeTd(this)'?>刪除</a></td>"; ????????table.appendChild(newNode); ????????changeBg(); ????} ?????//?創(chuàng)建刪除函數(shù) ????function?removeTd(node)?{ ????????node.parentNode.parentNode.parentNode.removeChild(node.parentNode.parentNode); ????} ??</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="removeTd(this)">刪除</a></td>???<!--在刪除按鈕上添加點擊事件??--> ??????</tr><tr> ??????<td>xh002</td> ??????<td>劉小芳</td> ??????<td><a?href="javascript:;"?onclick="removeTd(this)"?>刪除</a></td>???<!--在刪除按鈕上添加點擊事件??--> ??????</tr></table> ??????<input?type="button"?value="添加一行"?onclick="addTable()"/>???<!--在添加按鈕上添加點擊事件??--> ?</body> </html>
如題,我的代碼運行后,如果刪除中間的行,再點“添加一行”,新添加行的學號會連不上。
感覺可以用正則來獲取最后一行的學號,新行學號=最后行學號+1。js里是怎么使用正則呢。。。
2019-04-09
同問,刪除了第一行后,第二行的數(shù)字2怎么變成1