|
TOCControl
TOC (Table of Contents) controlJavaScript component initially inspired by A Dynamic Table of Contents Script by Matt Whitlock: For long, heavily-edited HTML documents, it becomes a major nuisance to keep a table of contents up to date. Perhaps even more annoying is keeping all of the section numbers contiguous and increasing. I offer a JavaScript solution here that dynamically builds a table of contents from the headings in a document and prepends legal-style section numbers to each of the headings. IntroductionThe TOC control dynamically builds a table of contents from the headings in a document and prepends legal-style section numbers to each of the headings:
UsageThis extension parses the given DOM node children to find existing headings, and numerates them according to their hierarchical structure. Note: In order for a TOC entry to have an anchor pointing to the corresponding heading, the id attribute is required on that element. InstallationAdd one of the following lines within the <head> tag of your HTML document: <!-- Normal version --> <script src="http://samaxesjs.googlecode.com/files/samaxesjs.toc-1.5.js"></script> <!-- Minified version --> <script src="http://samaxesjs.googlecode.com/files/samaxesjs.toc-1.5.min.js"></script> Put the TOC container where you want the table of contents to appear: <div id="toc"></div> Edit your HTML document and add the following code to a <script> block just before the </body> end tag: samaxesJS.toc(); Options
ExamplesProcesses heading levels 2 through 4 (default): samaxesJS.toc();
// same as: samaxesJS.toc({container: 'toc', exclude: 'h1, h5, h6', context: '', autoId: false, numerate: true});Processes all heading levels: samaxesJS.toc({exclude: ''});Processes heading levels 2 through 5: samaxesJS.toc({exclude: 'h1, h6'});Processes heading levels 2 through 5 inside an element with the id myElement: samaxesJS.toc({exclude: 'h1, h6', context: 'myElement'});Generate header's id attribute when it doesn't exist: samaxesJS.toc({autoId: true});Disable header's numeration generation (useful when using CSS counters): samaxesJS.toc({numerate: false});CSS StyleStyle your TOC to suppress the list markers that would normally adorn the list items, take this CSS snippet as an example: div#toc ul {
list-style: none;
}
div#toc ul li ul {
margin-bottom: 0.75em;
}
div#toc ul li ul li ul {
margin-bottom: 0.25em;
}Demohttps://samaxesjs.googlecode.com/svn/trunk/demo/toc-standalone.html | ||||||||||||||||||