js控制删除table的行

<html> <head> <mce:script language="javascript"><!-- var r=-1; function addrow(){ r++; //alert(r); var tbody = document.getElementById("tb"); var row = tbody.insertRow(); var innerHTML1 = "<input type='text' name='cusername' size='15' value=''>"; var innerHTML2 = "<input type='text' name='cpassword' size='15' value=''>"; var innerHTML3 = "<input type='button' name='cpassword2' value='删除' onclick='remove("+r+")'>"; row.insertCell(0).innerHTML = innerHTML1; row.insertCell(1).innerHTML = innerHTML2; row.insertCell(2).innerHTML = innerHTML3; tbody.appendChild(row); } function remove(ind){ //alert(ind); var tbody = document.getElementById("tb"); r--; var oTr = tbody.childNodes[ind]; // alert(tbody.childNodes.length); // alert(oTr.tagName); //alert(oTr.innerHTML); tbody.removeChild(oTr); var trRows = tbody.childNodes; for (var i = ind; i < trRows.length; i++) { var sibingTr = trRows[i]; //alert(sibingTr.innerHTML); if (sibingTr == null) { continue; } var cells = sibingTr.childNodes; for (var j = 0; j < cells.length; j++) { var oldContent = cells[j].innerHTML; if (j == 0) { var replaceRegx = "(/d+)/g"; oldContent = oldContent.replace(replaceRegx, "" + (i + 1)); } cells[j].innerHTML = ProcessStrIndex(oldContent, i); } //alert(sibingTr.innerHTML); } } function ProcessStrIndex(str, index) { //alert(index); var replaceRegx = //(/d+/)/g; str = str.replace(replaceRegx, "(" + index + ")"); replaceRegx = //[/d+/]/g; str = str.replace(replaceRegx, "[" + index + "]"); return str; } // --></mce:script> </head> <table border = "1" width="55%"> <tbody id = "tb"> <thead><td colspan = "3">addAndRemoveRows</td></thead> </tbody> <tr align = "left"><td align = "right" colspan="3"><input type="button" value="添加" onclick="addrow()"/></td></tr> </table> </html>

你可能感兴趣的:(js控制删除table的行)