Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 表中单元格和子项之间的差异_Javascript_Html_Dom_Html Table - Fatal编程技术网

Javascript 表中单元格和子项之间的差异

Javascript 表中单元格和子项之间的差异,javascript,html,dom,html-table,Javascript,Html,Dom,Html Table,以一个简单的表格为例,如: <table id="fu"> <tr> <th>A</th> <th>B</th> </tr> <tr> <td>1</td> <td>2</td> </tr> </table> 或 我理解子节点和子节点之间的区别,但细胞和子节点似

以一个简单的表格为例,如:

<table id="fu">
  <tr>
    <th>A</th>
    <th>B</th>
  </tr>
  <tr>
    <td>1</td>
    <td>2</td>
  </tr>
</table>

我理解子节点和子节点之间的区别,但细胞和子节点似乎是相同的。如果我在调试器中查看它们,我会看到:

 fu.children[0].children[0].
   ...
   cells: HTMLCollection(2) [th, th]
   childElementCount: 2
   childNodes: NodeList(5) [text, th, text, th, text]
   children: HTMLCollection(2) [th, th]
   ...
有什么区别?我应该使用.细胞还是.儿童?这无关紧要吗?这似乎并不重要,但我担心可能会有不同的情况,这会破坏我的代码


谢谢

因为表行可以拥有的唯一子元素是单元格,所以在
tr
元素上使用这两个元素时没有实际的区别


children
属性适用于DOM中的所有元素,但它来自较新的规范,即
cells
,因此在一些古老和过时的浏览器上不受支持。

因为表行可以拥有的唯一子元素是cells,在
tr
元素上使用时,两者之间没有实际区别


children
属性应用于DOM中的所有元素,但是它来自一个较新的规范,
单元格
,因此在一些古老和过时的浏览器上不受支持。

请使用
document.querySelector all
document.querySelector
。请使用
document.querySelector
document.querySelector
fu.children[0].children[0].children[0]
 fu.children[0].children[0].
   ...
   cells: HTMLCollection(2) [th, th]
   childElementCount: 2
   childNodes: NodeList(5) [text, th, text, th, text]
   children: HTMLCollection(2) [th, th]
   ...