JSTL -循环遍历表行并在每一行设置背景颜色



目的(根据forEach循环)是在表内每3行设置一个背景色。我的代码下面不工作。标签返回正常,所有数据在里面,但没有设置颜色。

<c:forEach var="coffee" items="${collection}">
<tr  class="${status.count % 3 == 0 ? 'even' : 'oneven'}"
${status.count % 3 == 0 ? 'even' : 'oneven'}  >
<td> ${coffee.brand} </td>
<td> ${coffee.type} </td>
<td> ${coffee.country} </td>
</tr>
</c:forEach>

我的CSS类

tr.even { background: red; }
tr.odd { background: green; }

谢谢你的帮助。

我找到了我的答案:

<h2>tabel with changing colors</h2>
    <table border=1>
        <tr>
            <th>Brand</th>
            <th>type</th>
            <th>Country</th>
        </tr>
        <c:forEach var="coffees" items="${collection}" varStatus="status">

            <tr class="${status.count % 3 == 0 ? 'even' : 'odd'}"
                ${status.count % 3 == 0 ? 'even' : 'odd'}>
                <td>${coffees.brand}</td>
                <td>${coffees.type}</td>
                <td>${coffees.country}</td>
            </tr>
            </c:forEach>

    </table>

status是一个未定义的属性。您需要使用forEach标签的varStatus属性来定义它:

<c:forEach var="coffee" items="${collection}" varStatus="status">

color也用于设置前景(文本)颜色。不设置背景色。并且您将类应用于li,因此它不适用于表行和单元格。CSS应该是:

tr.even td {
    background-color: #006699;
}
tr.oneven td {
    background-color: #009999; 
}

最新更新