开发者

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");
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜