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261.
parent()
Returnstheparent(s)ofoneormoreHTMLelements,whichcanbe:
l
Theelementsthatmatchtheselectorofascript(see"results"onpage844).
l
Oneelementthatmatchestheselectorofascriptthatrunsfor"Eachmatchedelement"(see
"this"onpage781and"Settingthescopeofascript"onpage373).
l
Theelementsreturnedbyaqueryinthetemplate(see"query()"onpage742).
InHTML,aparentisanelementthatcontainsanotherelement.
Togetanancestorthatmatchesaparticularselector,useclosest()(see"closest()"onpage795).
Togetanelement'ssiblingelement,youcanuse"prev()"onpage809or"next()"onpage802.
Example
AssumethattherearethreeparagraphsinaBoxandthatoneofthoseparagraphsmatchesthe
selectorofthisscript.Theparagraphisstoredintheresultsobject(see"results"onpage844).The
followingscriptretrievestheBox(whichistheparentoftheparagraph)usingresults.parent(),
andthenchangesitsbackgroundcolortored.
results.parent().css('background-color' , 'red');
Page 771