alternate row colors using jquery

后端 未结 6 1320
被撕碎了的回忆
被撕碎了的回忆 2021-02-05 18:38

Using jQuery and not CSS, is it possible to alternate row colors between records? If so can anyone provide a short code script on how to accomplish this?

6条回答
  •  [愿得一人]
    2021-02-05 19:26

    You can select tr elements from a table and css accepts a function as a parameter, which will return a value based on some criteria you decide. In this case, you can test the index for evenness.

    $("#table tr").css("background-color", function(index) {
        return index%2==0?"blue":"red";
    });
    

    JSFiddle: http://jsfiddle.net/n3Zny/

提交回复
热议问题