2022.2

Table Of Contents
i++;
}
// Go one step back unless we processed all items in our array.
if( i < products.length ) {
query("p:last-child", this).remove();
i--;
}
});
pageref()
Returnsamarkerthatwillbereplacedwiththeelement'spagenumberafterpagination.Thisonly
worksforelementsinthesectionthatiscurrentlybeingmerged.
Example
Creating a table of contents
Thefollowingscriptcreatesatableofcontentsforalllevel1headings(<h1>elements)withtheclass
titleinonesection.
var toc = '<ul ID="toc">';
query('h1.title').each(function()
{toc += '<li>' + this.text() + ' <span class="li_toc">' + this.pageref() + '</span></li>';
});
toc += '</ul>';
results.after(toc);
Thefirstlinecreatesavariableforthetableofcontents,whichwillbealist(a<ul>elementwiththeID
toc).Thestarttagofthelistisaddedtothevariable.
Thenextlinedoesaqueryforalllevel1headings(<h1>elements)withtheclasstitleinthecurrent
section.Witheach()thescriptloopsthroughthem.Foreachoftheheadingsitaddsalineitemtothe
list,withthetext(this.text())andthepagereferenceoftherespectiveheading.
Aftertheloop,theendtagofthelistisaddedtothevariable.
Finally,thescriptaddsthevariable-thatnowcontainsthetableofcontents-aftertheresults.The
resultsobjectcontainstheelementsthatmatchtheselectorofthescript.So,ifthescript'sselector
selectsthetitleofthetableofcontents,thetableofcontentswillbeaddedafterthat.
Thefollowingstylerules,addedtothestylesheet,willalignthechaptertitlestotheleftandthepage
numberstotheright:
#toc li {
text-align:left;
}
#toc span {
float: right;
}
Notethatthesestylesusethelist'sID,thatwasdefinedinthefirstlineofcode.Forinformationabout
stylesheets,see"StylingtemplateswithCSSfiles"onpage672.
Page 1227