How to alternate HTML table row colors using JSP?

后端 未结 6 1536
你的背包
你的背包 2020-12-05 17:35

How do I alternate HTML table row colors using JSP?

My CSS looks something like:

tr.odd {background-color: #EEDDEE}
tr.even {background-color: #EEEED         


        
6条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-05 18:07

    I don't use JSP, so I can't give you an answer in your language, but here's what I do (using pseudo code)

    counter = 0
    foreach (elements)
        counter = counter + 1
        output: ...
    

    Personally, I name the classes "row0" and "row1", which lets you alternate between them with a simple modulus calculation, also, if you decide to have rows alternating in triples or quads (instead of pairs), you can easily extend it to row2, row3 and change your output code to be counter % 4, etc.

提交回复
热议问题