Alternate background colors for list items

前端 未结 9 1849
一向
一向 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:21

    You can do it by specifying alternating class names on the rows. I prefer using row0 and row1, which means you can easily add them in, if the list is being built programmatically:

    for ($i = 0; $i < 10; ++$i) {
        echo '...';
    }
    

    Another way would be to use javascript. jQuery is being used in this example:

    $('table tr:odd').addClass('row1');
    

    Edit: I don't know why I gave examples using table rows... replace tr with li and table with ul and it applies to your example

提交回复
热议问题