A plugin that generates table of contents(TOC) dynamically for both jQuery or non-jQuery.
Install
bower install jquery-toc
Compatibility
- non-jQuery version works on IE9+
- jQuery version works on anywhere with jQuery
Showcase
https://idiotwu.me/understanding-es6-promise/
Usage
non-jQuery
// returns an ordered-list of headers in page contentvarlist =initTOC(options);// then you can append it to anywhere you likecontainer.appendChild(list);
jQuery
// will append TOC list to target element$(elem).initTOC(options);
Options
initTOC({selector:'h1, h2, h3, h4, h5, h6',scope:'body',overwrite:false,prefix:'toc'})
-
String:selector: headers selector, default is'h1, h2, h3, h4, h5, h6'. -
String:scope: selector to specify elements search scope, default is'body'. -
Boolean:overwrite: whether to overwrite existed headers' id, default isfalse, which means plugin will preserve the existed id property and create a string liketoc-1for those don't have id set. -
String:prefix: string to prepend to id/href property, default is'toc', which generates a string liketoc-0.
License
MIT.