jQuery-向LI添加第一个和最后一个类

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了jQuery-向LI添加第一个和最后一个类相关的知识,希望对你有一定的参考价值。

Since :first-child and :last-child aren't compatible with all modern browsers I thought I'd post this snippet. It make automating the class creation easier too instead of having to remember when typing up your html or creating logic in your programming.

Rename "first-item" and/or "last-item" to whatever class you want to use. You can change the "li" part of the code to whatever element your want or even remove it altogether.
  1. $(function(){
  2. $("li:first-child").addClass("first_item");
  3. $("li:last-child").addClass("last_item");
  4. });

以上是关于jQuery-向LI添加第一个和最后一个类的主要内容,如果未能解决你的问题,请参考以下文章