Alternate background colors for list items

前端 未结 9 1834
一向
一向 2020-12-04 07:42

I have a list, and each item is linked, is there a way I can alternate the background colors for each item?

9条回答
  •  伪装坚强ぢ
    2020-12-04 08:19

    If you want to do this purely in CSS then you'd have a class that you'd assign to each alternate list item. E.g.

    
    

    If your list is dynamically generated, this task would be much easier.

    If you don't want to have to manually update this content each time, you could use the jQuery library and apply a style alternately to each

  • item in your list:

    
    

    And your jQuery code:

    $(document).ready(function(){
      $('#myList li:nth-child(odd)').addClass('alternate');
    });
    

提交回复
热议问题