如何使用 JSP 交替 HTML 表格行颜色? [英] How to alternate HTML table row colors using JSP?

查看:30
本文介绍了如何使用 JSP 交替 HTML 表格行颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用 JSP 替换 HTML 表格行颜色?

How do I alternate HTML table row colors using JSP?

我的 CSS 看起来像:

My CSS looks something like:

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

我想使用 来迭代一个集合.

I want to use <c:forEach> to iterate over a collection.

<c:forEach items="${element}" var="myCollection">
  <tr>
    <td><c:out value="${element.field}"/></td>
    ...
  </tr>
</c:forEach>

我需要一个整数计数变量或布尔奇数/偶数变量来跟踪行.然后我的 <tr> 标签看起来像:

I need an int count variable or boolean odd/even variable to track the row. Then my <tr> tag would look something like:

<tr class="odd or even depending on the row">

推荐答案

forEach 标签上使用 varStatus 属性,JSTL 将管理 javax.servlet.jsp.jstl.core.LoopTagStatus 在你指定的变量名中.

Use the varStatus attribute on your forEach tag and JSTL will manage an instance of a javax.servlet.jsp.jstl.core.LoopTagStatus for you in the variable name you specify.

然后您可以使用三元运算符轻松输出适当的类名:

You can then use a ternary operator to easily output the appropriate class name:

<c:forEach items="${element}" var="myCollection" varStatus="loopStatus">
  <tr class="${loopStatus.index % 2 == 0 ? 'even' : 'odd'}">
    ...
  </tr>
</c:forEach>

来自 IBM 的这份 JSTL 入门提供了有关 core 标签库和它给你的东西.

This JSTL primer from IBM has more information about the core tag library and what it gives you.

这篇关于如何使用 JSP 交替 HTML 表格行颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆