|
Page 2 of 4
Writing the jQuery Code
Our custom code will go in the second, currently empty, JavaScript file which
we included from the HTML using <script src="dolittle.js" type="text/ javascript"></script>.
Despite how much it accomplishes, the script is fairly short:
jQuery.fn.toggleNext = function() { this.toggleClass('arrow-down') . next().slideToggle ('fast'); }; $(document).ready(function() { $('<div id="page-contents"></div>') .prepend('<h3>Page Contents</h3>') .append('<div></div>') .prependTo('body'); $ ('#content h2'). each(function(index ) { var $chapterTitle = $(this); var chapterId = 'chapter-' + (index + 1); $chapterTitle.attr('id', chapterId); $('<a></a>').text($chapterTitle.text()) .attr({ 'title': 'Jump to ' + $chapterTitle.text(), 'href': '#' + chapterId }) .appendTo('#page-contents div'); }); $('#page-contents h3').click(function() { $(this).toggleNext(); }); $('#introduction > h2 a').click(function() { $('#introduction').load(this.href); return false; }); });
jQuery.fn.toggleNext%20%3D%20function%28%29%20%7B%0D%0A%20%20%20this.toggleClass%28%27arrow-down%27%29%0D%0A%20%20%20%20%20%20.next%28%29.slideToggle%28%27fast%27%29%3B%0D%0A%7D%3B%0D%0A%24%28document%29.ready%28function%28%29%20%7B%0D%0A%20%20%20%24%28%27%3Cdiv%20id%3D%22page-contents%22%3E%3C%2Fdiv%3E%27%29%0D%0A%20%20%20%20%20%20.prepend%28%27%3Ch3%3EPage%20Contents%3C%2Fh3%3E%27%29%0D%0A%20%20%20%20%20%20.append%28%27%3Cdiv%3E%3C%2Fdiv%3E%27%29%0D%0A%20%20%20%20%20%20.prependTo%28%27body%27%29%3B%0D%0A%20%20%20%24%28%27%23content%20h2%27%29.each%28function%28index%29%20%7B%0D%0A%20%20%20%20%20%20var%20%24chapterTitle%20%3D%20%24%28this%29%3B%0D%0A%20%20%20%20%20%20var%20chapterId%20%3D%20%27chapter-%27%20%2B%20%28index%20%2B%201%29%3B%0D%0A%20%20%20%20%20%20%24chapterTitle.attr%28%27id%27%2C%20chapterId%29%3B%0D%0A%20%20%20%20%20%20%24%28%27%3Ca%3E%3C%2Fa%3E%27%29.text%28%24chapterTitle.text%28%29%29%0D%0A%20%20%20%20%20%20%20%20%20.attr%28%7B%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%27title%27%3A%20%27Jump%20to%20%27%20%2B%20%24chapterTitle.text%28%29%2C%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%27href%27%3A%20%27%23%27%20%2B%20chapterId%0D%0A%20%20%20%20%20%20%20%20%20%7D%29%0D%0A%20%20%20%20%20%20%20%20%20.appendTo%28%27%23page-contents%20div%27%29%3B%0D%0A%20%20%20%7D%29%3B%0D%0A%20%20%20%24%28%27%23page-contents%20h3%27%29.click%28function%28%29%20%7B%0D%0A%20%20%20%20%20%20%24%28this%29.toggleNext%28%29%3B%0D%0A%20%20%20%7D%29%3B%0D%0A%20%20%20%24%28%27%23introduction%20%3E%20h2%20a%27%29.click%28function%28%29%20%7B%0D%0A%20%20%20%20%20%20%24%28%27%23introduction%27%29.load%28this.href%29%3B%0D%0A%20%20%20%20%20%20return%20false%3B%0D%0A%20%20%20%7D%29%3B%0D%0A%7D%29%3B
We now have a dynamic table of contents that brings users to
the relevant portion of the text, and an introduction that is loaded on demand.
Script Dissection
NOTE: We will not discuss the operation of this script in much detail
here, but a similar script is presented as a tutorial on the Learning jQuery web
log: http://www.learningjquery.com/2007/06/ automatic-page-contents.
Selector Expressions
Before we can act on an HTML document, we need to locate the relevant
portions. In our script, we sometimes use a simple approach to finding an
element:
$('#introduction')
This expression creates a new jQuery object that references the element with
the ID introduction. On the other hand, sometimes we require a more intricate
selector:
$('#introduction > h2 a')
Here we produce a jQuery object potentially referring to many elements.
Elements are included if they are anchor tags, but only if they are descendants
of <h2> elements that are themselves children of an element with the ID
introduction.
These selector expressions can be as simple or complex as we need. Chapter 2
will enumerate all of the selectors available to us and how they can be
combined.
|