addClass on condition
HTML:
<ul id="list">
<li>item</li>
<li>item</li>
<li>item</li>
<li>item</li>
<li class="action">item</li>
</ul>
jQuery:开发者_高级运维
$("#list li:last").addClass("active");
This almost works, but the "action" item takes the "active" class when its present which I don't want. How do I rewrite the above jQuery so it always ignores the li.action
and assigns "active" class to the last li
before it??
Use the :not()
selector. E.g.
$("#list li:last:not(.action)").addClass("active");
精彩评论