document.write(tbody.nodeName);结果为TBODY呢,我HTML中没有这个节点啊?tbody = document.getElementById('myTable').lastChild; 为什么这里要加上lastChild呢?
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>
<body>
<table id="myTable" border="1px" width="60%" bgcolor="#fff">
<tr>
<th width="30px"><input type="checkbox" name="checkbox" /></th>
<th>学号</th>
<th>姓名</th>
<th>性别</th>
<th>操作</th>
</tr>
<tr bgcolor="#fff" id="tr1" onmousemove="bgColor()">
<td width="30px"><input type="checkbox" name="checkbox" /></td>
<td>xh001</td>
<td>王小明</td>
<td>男</td>
<td width="40px"><a href="javascript:;" onclick="deleteRow(this);">删除</a></td>
<!--在删除按钮上添加点击事件 -->
</tr>
<tr>
<td width="30px"><input type="checkbox" name="checkbox" /></td>
<td>xh002</td>
<td>刘小芳</td>
<td>女</td>
<td width="40px"><a href="javascript:;" onclick="deleteRow(this)">删除</a></td>
<!--在删除按钮上添加点击事件 -->
</tr>
</table>
节点名称:
<input id="sex1" type="text"/>
<script>
function deleteRow(obj){
var tbody = document.getElementById('myTable').lastChild; //为什么这里要加上lastChild呢?
var tr = obj.parentNode.parentNode;
var con=confirm("你确定删除此行吗");
document.getElementById('sex1').value=tbody.nodeName;
if(con){tbody.removeChild(tr);}
}
</script>
</body>
</html>