调用项目中已有的js,将表格的的样式设置成间隔行显示不同的颜色。
如下图:
代码如下:
<body class="shadow_bg02" οnlοad="initialize(document.getElementById('list_table'));">
<div class="DIVoverflow">
<table id="list_table" border="0" cellpadding="0" cellspacing="1" width="100%" style="font-size:12px;background: #CCCCCC;">
<!-- 如果从历史记录列表进入的话,则有返回按钮;如果是从审批历史查询界面进入的话则没有返回按钮 -->
<c:if test="${empty goBackBtn}">
<tr bgcolor="#FFFFFF"><td colspan="6" align ="right"><input type="button" value="返回" class="button_back" οnclick="goback()"/> </td></tr>
</c:if>
<tr bgcolor="#FFFFFF">
<td nowrap colspan="1"> <strong>名称:</strong></td><td colspan="5">${freeEval.creater_org}</td>
</tr>
<tr bgcolor="#FFFFFF">
<td nowrap> <strong>AAA:</strong></td>
<td colspan="5"> ${freeEval.managerOrganization }</td>
</tr>
<tr bgcolor="#FFFFFF">
<td nowrap > <strong>BB:</strong></td>
<td colspan="5"> ${freeEval.corpname }</td>
</tr>
<tr bgcolor="#FFFFFF">
<td nowrap> <strong>CC:</strong></td>
<td colspan="5"> ${freeEval.accountType }</td>
</tr>
</table>
</form>
</div>
</body>
initialize方法如下:
function initialize(tableElement) {
if(tableElement){
for(var i = 1; i<tableElement.childNodes[0].childNodes.length; i++){
if (i%2 == 0) {
tableElement.childNodes[0].childNodes[i].bgColor = "#f7f7f7";
} else {
tableElement.childNodes[0].childNodes[i].bgColor = "#ffffff";
}
}
}
}
没有得到想要出现的结果。
最后经检验alert(tableElement.childNodes[0].nodeType);发现弹出nodeType为8,通过查找文档知道Node是注释类型。
所以原因是:多了一行注释引起的结果。把<!-- 如果从历史记录列表进入的话,则有返回按钮;如果是从审批历史查询界面进入的话则没有返回按钮 -->去掉就成功了。
同时,tableElement.childNodes[0]代表的是table下面第一个tbody。
2012-05-28 增加备注:tableElement.childNodes[0]代表的是table下面的第一个节点,也就是说如果表格设置了<thead><tbody><tfoot>的话,则就是<thead><tbody><tfoot>三个中最前面的那个。如果表格没有设置<thead><tbody><tfoot>的话,则默认的把表格的内容放入<tbody>中,也就是tableElement.childNodes[0]代表table下面第一个tbody。
中设置的
记录下,希望以后不犯错了。