$("ul:contains('a[href$=.pdf]')").addClass("tragedy");
我在做这件事时遇到了麻烦。我也试过这个
$("ul").closest('a[href$=.pdf]').addClass("tragedy");
但它没有用。如果有人能帮助我,我会非常感激
这是HTML:
<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>
答案 0 :(得分:1)
您想要将类添加到<ul>
而不是链接本身;翻转顺序:
$('a[href$=".pdf"]').closest('ul').addClass("tragedy");