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]
...