jQuery plugin: tocBuildertocBuilder is a jQuery plugin that builds a table of contents from heading tags. If you don't want to use heading tags, you can use any other tag to which you apply the CSS class 'tocEntry' and a custom attribute in the form "data-tocLevel='1'". The plugin is applied to the div (or other element) that you want to contain a clickable table of contents. Your jQuery selector should return a single element; if your selector returns multiple elements, only the first is used. By default, the plugin will build a table of contents from tocEntry CSS classes, levels 1-6. For example: $('#myDiv').tocBuilder()
will locate all elements in your document that have a 'tocEntry' CSS class. For example, elements such as this: <p class='tocEntry' data-tocLevel='1'>Level 1 heading</p> <p class='tocEntry' data-tocLevel='2'>Level 2 heading</p> It will use the text of each target element to create a hyperlink in myDiv element. By default, it will also append a 'back' link from each element to the TOC so that you can navigate back, although back links can be turned off using the options. You can create multiple TOCs on separate elements. If you do this, it works best if you use different levels for each TOC, otherwise you get two 'back' links on each heading. DownloadstocBuilder.js,
the plugin. Options
MethodsMethods are called using the pattern recommended by the plugin authoring tutorial. That is, the method name is a string argument to the plugin. For example: $('#myDiv').tocBuilder('disable');
Arguments are passed after the method name: $('#myDiv').tocBuilder('disable', true);
The following methods are available:
CSStocBuilder assumes some CSS class names, which you can find in the tocBuilder.css download, above. The 'tocEntry' class is merely used as a selector class if you don't want to use heading tags. In addition, the plugin applies the following classes to content that it inserts into the DOM:
Short ExamplesThe following example would create a TOC in myDiv using levels 1-6. 'tocEntry' CSS classes are used as the selector: $('#myDiv').tocBuilder();
The following examples create two TOCs on separate divs: $('#myDiv').tocBuilder({ type: 'headings', startLevel: 1, endLevel: 3, backLinkText:
'Return to TOC 1' });$('#myOtherDiv').tocBuilder({ type: 'headings', startLevel: 4, endLevel: 6, backLinkText:
'Return to TOC 2' });
The following example would create a TOC using heading tags 1-6: $('#myDiv').tocBuilder({ type: 'headings'});
The following example would create a TOC using heading tags 2-4, with a back link of 'Return to TOC': $('#myDiv').tocBuilder({ type: 'headings', startLevel: 2, endLevel: 4, backLinkText:
'Return to TOC' });
The following example would create a TOC without back links: $('#myDiv').tocBuilder({ insertBackLinks: false });
The following example would create a TOC using standard headings but would call the tocCallback function in order to retrieve the heading text. $('#myDiv').tocBuilder({ type: 'headings', textCallback: tocCallback });
Longer Examples |
|