Alternate background colors for list items

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

    If you use the jQuery solution it will work on IE8:

    jQuery

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

    CSS

    .alternate {
    background: black;
    }
    

    If you use the CSS soloution it won't work on IE8:

    li:nth-child(odd) {
        background: black;
    }
    

提交回复
热议问题