js删除节点问题,点击添加一行按钮添加行成功,但是点击删除按钮没有反应。
<scripttype="text/javascript">functioninsertRow(){vartr=document.createElement("tr");...
<script type="text/javascript"> function insertRow(){ var tr = document.createElement("tr"); var table = document.getElementsByTagName("table")[0]; var td1 = document.createElement("td"); tr.insertBefore(td1,null); td1.innerHTML = "001"; var td2 = document.createElement("td"); tr.insertBefore(td2,td3); td2.innerHTML = "孙悟空"; var td3 = document.createElement("td"); tr.appendChild(td3); td3.innerHTML ="男"; table.appendChild(tr); body.appendChild(table);
} function deleteRow(){ var tr = getElementsByTagName("tr"); var table = document.getElementsByTagName("table")[0]; table.removeChild(tr[tr.length-1]); } </script></head><body> <button onclick="insertRow()">添加一行</button><button onclick="deleteRow()">删除</button> <br/> <table> <tr> <td>学号</td><td>姓名</td><td>性别</td> </tr> </table></body> 展开
} function deleteRow(){ var tr = getElementsByTagName("tr"); var table = document.getElementsByTagName("table")[0]; table.removeChild(tr[tr.length-1]); } </script></head><body> <button onclick="insertRow()">添加一行</button><button onclick="deleteRow()">删除</button> <br/> <table> <tr> <td>学号</td><td>姓名</td><td>性别</td> </tr> </table></body> 展开
展开全部
<script type="text/javascript">
function insertRow(){
var tr = document.createElement("tr");
var table = document.getElementsByTagName("table")[0];
var td1 = document.createElement("td");
tr.insertBefore(td1,null);
td1.innerHTML = "001";
var td2 = document.createElement("td");
tr.insertBefore(td2,td3);
td2.innerHTML = "孙悟空";
var td3 = document.createElement("td");
tr.appendChild(td3);
td3.innerHTML ="男";
table.appendChild(tr);
document.body.appendChild(table); //少了document
}
function deleteRow(){
var tr = document.getElementsByTagName("tr"); //主要是这里少了个document 导致不能删除
var table = document.getElementsByTagName("table")[0];
table.removeChild(tr[tr.length-1]);
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询