<TABLE ID="tableNode">
<TR><TD BGCOLOR="yellow">This is row 1, cell 1</TD><TD BGCOLOR="orange">This is row 1, cell 2</TD></TR>
<TR><TD BGCOLOR="red">This is row 2, cell 1</TD><TD BGCOLOR="magenta">This is row 2, cell 2</TD></TR>
<TR><TD BGCOLOR="lightgreen">This is row 3, cell 1</TD><TD BGCOLOR="beige">This is row 3, cell 2</TD></TR>
</TABLE>
在分析它的DOM结构前,我们要为这段代码添加上一对<TBODY>标记:
<TABLE ID="tableNode">
<TBODY>
中间代码略
</TBODY>
</TABLE>
![](http://www.chinabyte.net/20011102/yl-011102-dom-3-s.gif)
右面是上述HTML文档的DOM Tree图示,可点击放大。
包含表格文档的节点导航
现在我们来看看如何在包含表格的文档中进行DOM节点导航。同样,正确地理解上面所示的DOM Tree图示,有助于我们清晰地看懂导航节点表达式。
起始节点
|
到达节点
|
寻址表达式
|
<TABLE> | 表格的第2行 | tableNode.firstChild.childNodes[1] |
表格的第2行的第1个单元 | tableNode.firstChild.childNodes[1].childNodes[0] | |
第1行第2个单元的内容 | tableNode.firstChild.firstChild.childNodes[1].firstChild | |
表格第3行 | <TABLE> | tr3Node.parentNode.parentNode或 tr3Node.previousSibling.parentNode.parentNode或 tr3Node.previousSibling.previousSibling.parentNode.parentNode |
<TABLE> | 分别到达6个单元格 | tableNode.firstChild.firstChild.firstChild.firstChild tableNode.firstChild.firstChild.childNodes[1].firstChild tableNode.firstChild.childNodes[1].firstChild.firstChild tableNode.firstChild.childNodes[1].childNodes[1].firstChild tableNode.firstChild.childNodes[2].firstChild.firstChild tableNode.firstChild.childNodes[2].childNodes[1].firstChild |