About the answer:
$ (function () {
First little question
(1) Add special class to all <li> elements located at the second level of the nested list;
//$ (' #selected-plays > li > Ul > Li '). addclass (' special ');
Second small question
(2) Add the year class to all cells in the third column of the table;
//$ (' tr '). Find (' Td:eq (2) '). AddClass (' year ');
The third small question
(3) Add special class for the first line containing text tragedy in the table;
//$ (' Td:contains (Tragedy) '). EQ (0). Parent (). addclass (' special ');
Or
$ (' Td:contains (Tragedy) '). Parent (). Filter (' Tr:eq (0) '). addclass (' special ');
(4) Challenge: Select all the list items (<li> elements) that contain the link (<a>) and add the Afterlink class to the sibling list item element for each selected list item;
//$ (' a '). Parent (). Parent (). Children (). Not (' Li:has (a) '). addclass (' Afterlink ');
Or
$ (' a '). Parent (). siblings (). Not (' Li:has (a) '). addclass (' Afterlink ');
(5) Challenge: Add the tragedy class to the closest ancestor element to the. pdf link <ul>.
$ (' a[href$= ". pdf"]). Parents (' ul:eq (0) '). AddClass (' tragedy ');
})
Book matching source code HTML:
<! DOCTYPE html>
<meta charset= "Utf-8" >
<title>selected Shakespeare plays</title>
<link rel= "stylesheet" href= "Css/02.css" type= "Text/css"/>
<script type= "Text/javascript" src= "Jquery.js" ></script>
<script src= "Js/02.js" ></script>
<body>
<div id= "Container" >
<ul id= "Selected-plays" class= "Clear-after" >
<li>comedies
<ul>
<li><a href= "/asyoulikeit/" >as you like it</a></li>
<li>all ' s well, Ends well</li>
<li>a Midsummer Night ' s dream</li>
<li>twelfth night</li>
</ul>
</li>
<li>tragedies
<ul>
<li><a href= "Hamlet.pdf" >Hamlet</a></li>
<li>Macbeth</li>
<li>romeo and Juliet</li>
</ul>
</li>
<li>histories
<ul>
<li>henry IV (<a href= "Mailto:[email protected]" >email</a>)
<ul>
<li>part i</li>
<li>part ii</li>
</ul>
</li>
<li><a href= "http://www.shakespeare.co.uk/henryv.htm" >henry v</a></li>
<li>richard ii</li>
</ul>
</li>
</ul>
<table>
<tr>
<td>as you like it</td>
<td>Comedy</td>
<td></td>
</tr>
<tr>
<td>all ' s well, Ends well</td>
<td>Comedy</td>
<td>1601</td>
</tr>
<tr>
<td>Hamlet</td>
<td>Tragedy</td>
<td>1604</td>
</tr>
<tr>
<td>Macbeth</td>
<td>Tragedy</td>
<td>1606</td>
</tr>
<tr>
<td>romeo and Juliet</td>
<td>Tragedy</td>
<td>1595</td>
</tr>
<tr>
<td>henry IV, part i</td>
<td>History</td>
<td>1596</td>
</tr>
<tr>
<td>henry v</td>
<td>History</td>
<td>1599</td>
</tr>
</table>
<table>
<tr>
<td>the Fair youth</td>
<td>1–126</td>
</tr>
<tr>
<td>the Dark lady</td>
<td>127–152</td>
</tr>
<tr>
<td>the Rival poet</td>
<td>78–86</td>
</tr>
</table>
</div>
</body>
CSS for book matching:
/***************************************
Default Styles
************************************** */
HTML, Body {
margin:0;
padding:0;
}
Body {
font:62.5% Verdana, Helvetica, Arial, Sans-serif;
Color: #000;
Background: #fff;
}
#container {
Font-size:1.2em;
margin:10px 2em;
}
H1 {
Font-size:2.5em;
margin-bottom:0;
}
H2 {
Font-size:1.3em;
Margin-bottom:. 5em;
}
h3 {
Font-size:1.1em;
margin-bottom:0;
}
Code {
Font-size:1.2em;
}
A
Color: #06581f;
}
/***************************************
Chapter Styles
************************************** */
H2 {
Clear:left;
}
Li {
Padding:0 3px;
Color: #000;
}
. horizontal {
Float:left;
List-style:none;
margin:10px;
}
. sub-level {
Background: #ccc;
}
A
Color: #00c;
}
A.mailto {
Background:url (images/email.png) no-repeat 100% 2px;
padding-right:18px;
}
A.pdflink {
Background:url (images/pdf.png) no-repeat 100% 0;
padding-right:18px;
}
A.henrylink {
Background-color: #fff;
padding:2px;
border:1px solid #000;
}
a.external {
Background: #fff URL (images/external.png) no-repeat 100% 2px;
padding-right:16px;
}
Ul.tragedy {
border:1px solid #999;
}
Li.afterlink {
BORDER-RIGHT:4PX solid #090;
}
Table {
Border-collapse:collapse;
}
Th, TD {
Text-align:left;
PADDING:2PX 4px;
}
. table-heading {
Background-color: #000;
Color: #fff;
}
. alt {
Background-color: #ccc;
}
. Highlight {
Font-weight:bold;
Font-style:italic;
}
. Italic {
Font-style:italic;
}
. Bold {
Font-weight:bold;
}
. Special {
Color: #f00;
}
. Year {
Background-color: #888;
Color: #fff;
Padding:0 10px;
Text-align:center;
}
The basic jquery Tutorial (Fourth Edition), Chapter 2nd, exercises: