先看一段代码,我在样式里用 expression 动态运算,以达到表格的隔行换色效果(注明:样式里的 expression 用法目前只有IE浏览器支持!):
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>expression vs XML data</title>
<style type='text/css'>
.mm tr
{
background-color:expression((this.rowIndex%2==0)?"#00FF33":"#CCFF33")
}
</style>
<xml id='XmlData'>
<?xml version="1.0" encoding="GB2312"?>
<rows>
<row>
<id>1</id>
<area>Web 开发/非技术区</area>
<trend>↑</trend>
<percent>67.23%</percent>
</row>
<row>
<id>2</id>
<area>C/C++/C++ 语言</area>
<trend>↑</trend>
<percent>14.35%</percent>
</row>
<row>
<id>3</id>
<area>Delphi/非技术区</area>
<trend>↑</trend>
<percent>13.88%</percent>
</row>
<row>
<id>4</id>
<area>Oracle/基础和管理</area>
<percent>11.87%</percent>
<trend>↑</trend>
</row>
<row>
<id>5</id>
<area>Windows专区/Windows NT/2000/XP/2003</area>
<percent>8.33%</percent>
<trend>↑</trend>
</row>
<row>
<id>6</id>
<area>.NET技术/非技术区</area>
<percent>7.32%</percent>
<trend>↓</trend>
</row>
<row>
<id>7</id>
<area>Web 开发/JavaScript</area>
<percent>7.24%</percent>
<trend>↑</trend>
</row>
<row>
<id>8</id>
<area>VC/MFC/基础类</area>
<percent>4.80%</percent>
<trend>↑</trend>
</row>
<row>
<id>9</id>
<area>Java/J2EE / EJB / JMS</area>
<percent>3.00%</percent>
<trend>↑</trend>
</row>
<row>
<id>10</id>
<area>扩充话题/灌水乐园</area>
<percent>1.75%</percent>
<trend>↓</trend>
</row>
</rows>
</xml>
</head>
<body>
<table class="mm" id="Tab" datasrc='#XmlData' border='1'>
<tr>
<td><div datafld='id'></div></td>
<td><div datafld='area'></div></td>
<td><div datafld='trend'></div></td>
<td><div datafld='percent'></div></td>
</tr>
</table>
<hr>
下面这个表格是一个测试表格,以测试样式里的 expression 的代码准确性,
<table class="mm" border='1'>
<tr>
<td><div>1111</div></td>
<td><div>2222</div></td>
<td><div>3333</div></td>
<td><div>4444</div></td>
</tr>
<tr>
<td><div>2222</div></td>
<td><div>2222</div></td>
<td><div>3333</div></td>
<td><div>4444</div></td>
</tr>
<tr>
<td><div>3333</div></td>
<td><div>2222</div></td>
<td><div>3333</div></td>
<td><div>4444</div></td>
</tr>
<tr>
<td><div>4444</div></td>
<td><div>2222</div></td>
<td><div>3333</div></td>
<td><div>4444</div></td>
</tr>
</table>
</body>
</html>
可以看到测试结果,第一个表格竟然没有达到我最初想要的隔行换色效果!是样式有问题吗?显然不是,因为第二个表格很正常地显示了效果。我分析这个问题可能是渲染的前后顺序有关。即在数据绑定之前,表格里只有一行,CSS对表格进行了背景色的渲染,之后数据绑定操作,(不知是完全拷贝这个第一行的渲染效果还是 expression 不认后来加进去的行还是其它什么原因),总之后续添加的行只延用了第一行的样式。
为此我做了一个调整,即让样式渲染发生成数据绑定之后,我在<body>里的onload事件里加了一点代码。看一下代码效果:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>expression vs XML data</title>
<style type='text/css'>
.mm tr
{
background-color:expression((this.rowIndex%2==0)?"#00FF33":"#CCFF33")
}
</style>
<xml id='XmlData'>
<?xml version="1.0" encoding="GB2312"?>
<rows>
<row>
<id>1</id>
<area>Web 开发/非技术区</area>
<trend>↑</trend>
<percent>67.23%</percent>
</row>
<row>
<id>2</id>
<area>C/C++/C++ 语言</area>
<trend>↑</trend>
<percent>14.35%</percent>
</row>
<row>
<id>3</id>
<area>Delphi/非技术区</area>
<trend>↑</trend>
<percent>13.88%</percent>
</row>
<row>
<id>4</id>
<area>Oracle/基础和管理</area>
<percent>11.87%</percent>
<trend>↑</trend>
</row>
<row>
<id>5</id>
<area>Windows专区/Windows NT/2000/XP/2003</area>
<percent>8.33%</percent>
<trend>↑</trend>
</row>
<row>
<id>6</id>
<area>.NET技术/非技术区</area>
<percent>7.32%</percent>
<trend>↓</trend>
</row>
<row>
<id>7</id>
<area>Web 开发/JavaScript</area>
<percent>7.24%</percent>
<trend>↑</trend>
</row>
<row>
<id>8</id>
<area>VC/MFC/基础类</area>
<percent>4.80%</percent>
<trend>↑</trend>
</row>
<row>
<id>9</id>
<area>Java/J2EE / EJB / JMS</area>
<percent>3.00%</percent>
<trend>↑</trend>
</row>
<row>
<id>10</id>
<area>扩充话题/灌水乐园</area>
<percent>1.75%</percent>
<trend>↓</trend>
</row>
</rows>
</xml>
</head>
<body οnlοad="document.getElementById('Tab').className='mm'">
<table id="Tab" datasrc='#XmlData' border='1'>
<tr>
<td><div datafld='id'></div></td>
<td><div datafld='area'></div></td>
<td><div datafld='trend'></div></td>
<td><div datafld='percent'></div></td>
</tr>
</table>
<hr>
下面这个表格是一个测试表格,以测试样式里的 expression 的代码准确性,
<table class="mm" border='1'>
<tr>
<td><div>1111</div></td>
<td><div>2222</div></td>
<td><div>3333</div></td>
<td><div>4444</div></td>
</tr>
<tr>
<td><div>2222</div></td>
<td><div>2222</div></td>
<td><div>3333</div></td>
<td><div>4444</div></td>
</tr>
<tr>
<td><div>3333</div></td>
<td><div>2222</div></td>
<td><div>3333</div></td>
<td><div>4444</div></td>
</tr>
<tr>
<td><div>4444</div></td>
<td><div>2222</div></td>
<td><div>3333</div></td>
<td><div>4444</div></td>
</tr>
</table>
</body>
</html>
结果证明了我当初的设想,由此可见IE的样式 expression 和 XML数据岛绑定之间的配合不是很默契!