table的子节点问题,这个table的子节点问什么只有2个?

来源:9-8 访问子节点childNodes

qq_枫_

2016-11-07 13:02



<!DOCTYPE html>

<html>

 <head>

  <title> new document </title>  

  <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>   


 </head> 

 <body> 

  <table border="1" width="50%" id="table">

  <tr>

<th>学号</th>

<th>姓名</th>

<th>操作</th>

  </tr>  


  <tr>

<td>xh001</td>

<td>王小明</td>

<td><a href="javascript:;" onclick="deleteRow(this)">删除</a></td>

  </tr>


  <tr>

<td>xh002</td>

<td>刘小芳</td>

<td><a href="javascript:;" onclick="deleteRow(this)">删除</a></td>

  </tr>  


  </table>

  


  <script type="text/javascript">  




var mytr=document.getElementById("table").childNodes;

          

document.write(mytr.length);

  </script> 


 </body>

</html>


写回答 关注

2回答

  • 浮生凉
    2016-11-07 16:17:01
    已采纳

    自己看审查元素,你写得不标准,浏览器给你加个tbody,还有一个一个子元素应该是浏览器给它加了个换行符。

    我们在html上这样写的话是长度是3个(下图)

    5820377a00014dc305000350.jpg


    而这样写的话长度是1(下图)

    582037ae0001c0ad05000333.jpg

    总之:反正换行符是算一个的。与其用childNodes这种方式不如用getElementByTagName("tr")。

    qq_枫_

    非常感谢!

    2016-11-07 16:34:22

    共 2 条回复 >

  • 慕村1994845
    2016-12-10 22:27:22

    浏览器给你的代码加了tbody之后,像是td tr 就不再是table的子节点,而是tbody的子节点了

JavaScript进阶篇

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

467393 学习 · 21877 问题

查看课程

相似问题