<!doctype html><html><head><meta charset="utf-8"><title>無標題文檔</title></head><body><p>請輸入姓名:<input type="text" id="text">? ?請輸入年齡:<input type="text" id="age">? ?<input type="button" value="添加" onclick="add()">? ?</p><script>? ? ? ? ? var text=document.getElementById("text"); ? var age=document.getElementById("age"); ? var table=document.createElement("table");? ? ? ? ? ?table.border=1;? ? ? ? ? ?table.width="800"; ? table.cellSpacing="0"; ? var tbody=document.createElement("tbody"); ? table.appendChild(tbody); ? var ten=document.createTextNode("ID"); ? var tena=document.createTextNode("姓名"); ? var tenb=document.createTextNode("年齡"); ? var tenc=document.createTextNode("操作"); ?? ? tbody.insertRow(0); ? tbody.rows[0].insertCell(0); ? tbody.rows[0].cells[0].appendChild(ten); ? tbody.rows[0].insertCell(1); ? tbody.rows[0].cells[1].appendChild(tena); ? tbody.rows[0].insertCell(2); ? tbody.rows[0].cells[2].appendChild(tenb); ? tbody.rows[0].insertCell(3); ? tbody.rows[0].cells[3].appendChild(tenc); ? document.body.appendChild(table); ? var i=1; function add(){? ? ? ? var j=0; ? var ten=document.createTextNode(i); ? var tena=document.createTextNode(text.value); ? var tenb=document.createTextNode(age.value); ? var tenc=document.createElement("input"); ? ? ? tenc.value="刪除"; ? tenc.type="button"; ? tenc.onclick="del(this)"; ? ? ? tbody.insertRow(i); ? tbody.rows[i].insertCell(j); ? tbody.rows[i].cells[j].appendChild(ten); ? tbody.rows[i].insertCell(j+1); ? tbody.rows[i].cells[j+1].appendChild(tena); ? tbody.rows[i].insertCell(j+2); ? tbody.rows[i].cells[j+2].appendChild(tenb); ? tbody.rows[i].insertCell(j+3); ? tbody.rows[i].cells[j+3].appendChild(tenc); ? ? ? i++;j++; ? }? ? function del(obj){? ? ? ? ? ?var fix=obj.parentNode.parentNode.parentNode; ? var ok=obj.parentNode.parentNode; ? ? ? fix.removeChild(ok); if(tbody.length==1){i=0} ?? ?} ?</script></body></html>
現在要求點擊刪除鍵時,刪除當前這一行,不知道代碼問題出在哪里,求大神指導
Echo_Chien
2016-08-05 16:21:54