alternate row colors using jquery

后端 未结 6 1333
被撕碎了的回忆
被撕碎了的回忆 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:35

    jQuery uses the Sizzle Seletor Engine, which is cool because it uses the same syntax as CSS. So you use the same selector as CSS but then use the jQuery .css() function to alter the elemen't CSS:

    $('#table_id').find('tr:even').css({'background-color':'red'})
                  .end().find('tr:odd').css({'background-color':'blue'});
    

    This code example selects the table you want to alter, then selects all the even child elements (tr's) and changes their background color, it then uses .end() to return to the previous selection of the entire table and selects the odd rows to alter their CSS.

提交回复
热议问题