document.write(tbody.nodeName);结果为TBODY呢,我HTML中没有这个节点啊?tbody = document.getElementById('myTable').lastChild; 为什么这里要加上lastChild呢?

来源:9-22 编程练习

天涯蝶舞

2016-08-02 15:22

<!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>



写回答 关注

2回答

  • couragefool3653558
    2016-08-08 13:47:12
    已采纳

    http://www.imooc.com/qadetail/154879

    已经有人问过这个问题了。。。这是链接。

    天涯蝶舞

    非常感谢!

    2016-08-09 11:40:45

    共 1 条回复 >

  • lxlacie
    2016-08-02 16:15:00

    浏览器会自动帮你添加,就跟如果你不写 <html><head></head></html>,浏览器会自动帮你添加一样,这是DOM树的标准。

JavaScript进阶篇

本课程从如何插入JS代码开始,带您进入网页动态交互世界

468783 学习 · 22528 问题

查看课程

相似问题