jquery遍历问题

144
提问者 60.186.33.19
2017-09-20 16:44 悬赏 38金币 阅读 277回答 0
操作

《jquery 基础教程(第四版)》第二章后面的练习:
(4) 挑战:选择包含链接()的所有列表项(<li>元素),为每个选中的列表项的同辈列
表项元素添加afterlink类;

HTML:

<!DOCTYPE html>  <html lang="en">   <head>     <meta charset="utf-8">     <title>Selected Shakespeare Plays</title>      <link rel="stylesheet" href="02.css" type="text/css" />      <script src="jquery.js"></script>     <script src="02.js"></script>   </head>   <body>     <div id="container">       <h2>Selected Shakespeare Plays</h2>       <ul id="selected-plays" class="clear-after">         <li>Comedies           <ul>             <li><a href="/asyoulikeit/">As You Like It</a></li>             <li>All"s Well That 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:henryiv@king.co.uk">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>        <h2>Shakespeare"s Plays</h2>       <table>         <tr>           <td>As You Like It</td>           <td>Comedy</td>           <td></td>         </tr>         <tr>           <td>All"s Well that 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>       <h2>Shakespeare"s Sonnets</h2>       <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> </html> 

jS:

  $("li").has("a").siblings().addClass("afterlink");

为什么不包含的<li>也是配到了?

程序员问题
相关问题
查看全部
收藏的人(6)