2022.2

Table Of Contents
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.
parent()
Returnstheparent(s)ofoneormoreHTMLelements,whichcanbe:
l
Theelementsthatmatchtheselectorofascript(see"results"onpage1301).
l
Oneelementthatmatchestheselectorofascriptthatrunsfor"Eachmatchedelement"(see
"this"onpage1237and"Settingthescopeofascript"onpage813).
l
Theelementsreturnedbyaqueryinthetemplate(see"query()"onpage1197).
InHTML,aparentisanelementthatcontainsanotherelement.
Togetanancestorthatmatchesaparticularselector,useclosest()(see"closest()"onpage1251).
Togetanelement'ssiblingelement,youcanuse"prev()"onpage1264or"next()"onpage1258.
Example
AssumethattherearethreeparagraphsinaBoxandthatoneofthoseparagraphsmatchesthe
selectorofthisscript.Theparagraphisstoredintheresultsobject(see"results"onpage1301).The
followingscriptretrievestheBox(whichistheparentoftheparagraph)usingresults.parent(),
andthenchangesitsbackgroundcolortored.
results.parent().css('background-color' , 'red');
Page 1261