简体   繁体   English

HTML表的DOM表示

[英]DOM representation of an HTML table

I'm just writing some code to traverse the DOM for a table, and I got a surprise. 我只是编写了一些代码来遍历一个表的DOM,我得到了一个惊喜。 The DOM (in FF, at least) isn't what I'd expected. DOM(至少在FF中)不是我所期望的。 FF has inserted a tbody and various text nodes, which just contain a "\\n". FF插入了一个tbody和各种文本节点,它们只包含一个“\\ n”。 The source HTML is: 源HTML是:

<table>
<tr>
<th></th><th>Jan</th><th>Feb</th><th>Mar</th><th>Apr</th><th>May</th><th>Jun</th><th>Jul</th><th>Aug</th><th>Sep</th><th>Oct</th><th>Nov</th><th>Dec</th>
</tr>
<tr><th>2010</th>
<td id='tdid0'>53</td>   <td id='tdid1'>249</td>  <td id='tdid2'>1689</td>  <td id='tdid3'>22753</td>
<td id='tdid4'>23051</td><td id='tdid5'>23633</td><td id='tdid6'>23923</td> <td id='tdid7'>23306</td>
<td id='tdid8'>23943</td><td id='tdid9'>24196</td><td id='tdid10'>24440</td><td id='tdid11'>24156</td>
</tr>
<tr><th>2011</th>
<td id='tdid12'>24262</td><td id='tdid13'>22554</td><td id='tdid14'>25507</td><td id='tdid15'>23144</td>
<td id='tdid16'>24354</td><td id='tdid17'>24610</td><td id='tdid18'>24870</td><td id='tdid19'>24424</td>
<td id='tdid20'>24698</td><td id='tdid21'>22640</td><td></td><td></td></tr>
</table>

1 - FF has inserted a tbody as the first child of the table , so I have to go up 3 levels (from a td ) to find the table , not 2 1 - FF插入了一个tbody作为table的第一个子,所以我必须上升3级(从td )找到table ,而不是2

2 - The html view in Firebug shows 3 rows, as expected, where rows 2 and 3 have a th followed by 12 td s. 2 - Firebug中的html视图按预期显示3行,其中第2行和第3行具有th后跟12 td However, when single-stepping through, it turns out that the tbody has 6 children, with a NL text node after each row 然而,当单步执行时,结果是tbody有6个子节点,每行后面有一个NL文本节点

3 - Firebug claims that each row has 13 children, as expected (a header and 12 data items). 3 - Firebug声称每行有13个孩子,正如预期的那样(标题和12个数据项)。 when stepping through the children with nextSibling , though, it turns out that the expected th and td elements are actually separated by text nodes, containing WS. 然而,当使用nextSibling逐步执行子节点时,事实证明,预期的thtd元素实际上是由包含WS的文本节点分隔的。 Any idea what's going on? 知道发生了什么事吗? Can the browser basically do what it wants? 浏览器可以基本上做它想要的吗? Thanks. 谢谢。

FF has inserted a tbody FF插入了一个tbody

In HTML 4 a tr element may not be a child of a table element. 在HTML 4中,tr元素可能不是表元素的子元素。 You need a tbody, thead or tfoot element in between. 你需要一个tbody,thead或tfoot元素。 The start and end tags of a tbody element are optional though. tbody元素的开始结束标记是可选的。 Firefox is correctly inserting it. Firefox正确插入它。

it turns out that the tbody has 6 children, with a NL text node after each row 事实证明,tbody有6个子节点,每行后面有一个NL文本节点

You have white space there in your markup 您的标记中有空白区域

it turns out that the expected th and td elements are actually separated by text nodes, containing WS 事实证明,预期的th和td元素实际上是由包含WS的文本节点分隔的

Ditto 同上

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM