How Auto-generate A Table Of Contents
I'm new to JavaScript, and for school I have to automatically make every <\h1> on my page generate into an 'ol' with in every 'li' a link to the place on my page where that h
Solution 1:
I'm not going to do your homework, but will point you in a good direction.
Try building an object containing the id and the title of each entry. From there, you can use that object to build practically anything, including an ordered list.
Of course, you can hard-rewrite the h1 tags into list items, but that's just not the proper way to do it and not something you should learn from.
Solution 2:
A start hint: You could do it with the help of jquery.
Like this HTML:
<olid=contents></ol><h1>Test</h1>
bla bla
<h1> Test2 </h1>
Ble ble ble
Jquery:
$(document).ready(function(){
$("h1").each(function(){
$("#contents").append("<li>" + $(this).html() + "</li>");
});
});
Post a Comment for "How Auto-generate A Table Of Contents"