|
||
| Inside Technique : W3C DOM Table of Contents : Finding an Element's Contents Unfortunately the W3C recommendation does not include any easy to use property or
function for obtaining the contents of an element. Instead, the contents are buried
beneath a text node object. For example, take the following simple HTML:
In the Internet Explorer model, the contents of this paragraph can be retrieved
using the innerText property of the P element. The W3C recommendation instead requires
you to manipulate each piece of text as a separate object. The above HTML fragment
is exposed as a tree of objects:
To retrieve the contents of this paragraph you need to traverse the object hierarchy
and extract the text in each text-node object. We wrote a recursive function, getTextForElement(), that
walks the object hierarchy for an element and returns the content:
The last step is to insert the table of contents into the document. The On the last page, we provide the complete script for generating the table of contents. The table of contents created by this article is not interactive. You can't click on an entry and navigate to the element. We will explain how to make this table of contents interactive next week. Stay tuned! Page 1:W3C DOM Table of Contents © 1997-2000 InsideDHTML.com, LLC. All rights reserved. |