如何为相同元素的表格单元格组着色
对于包含数字的 HTML 表格,我正在寻找一种优雅的 JavaScript/CSS 方法来识别每列中相等值的组,并相应地为相应单元格的背景着色。我将在回归测试结果的网络演示中使用它。
在 python 中,我可能会使用类似 itertools.groupby() 的东西。
为了说明这一点,我提供了一个屏幕截图示例和相应的 HTML 代码(手动构建)。
<head>
<style>
td {font-family: Monospace; font-size:16; }
</style>
</head>
<body>
<table border=1>
<tr><td>1.111</td></tr>
<tr><td>1.111</td></tr>
<tr><td bgcolor="LightBlue">2.222</td></tr>
<tr><td>1.111</td></tr>
<tr><td bgcolor="LightBlue">2.222</td></tr>
<tr><td> 1.111</td></tr>
<tr><td bgcolor="LightBlue">2.222</td></tr>
<tr><td bgcolor="Goldenrod">3.333</td></tr>
<tr><td> 1.111</td></tr>
</table>
</body>
For an HTML table with numbers, I am looking for an elegant JavaScript/CSS way to identify groups of equal values in each column, and color the background of corresponding cells accordingly. I will use it in a web presentation of regression test results.
In python I would probably have used something like itertools.groupby().
To illustrate, I include a screenshot example and the corresponding HTML code (constructed manually).
<head>
<style>
td {font-family: Monospace; font-size:16; }
</style>
</head>
<body>
<table border=1>
<tr><td>1.111</td></tr>
<tr><td>1.111</td></tr>
<tr><td bgcolor="LightBlue">2.222</td></tr>
<tr><td>1.111</td></tr>
<tr><td bgcolor="LightBlue">2.222</td></tr>
<tr><td> 1.111</td></tr>
<tr><td bgcolor="LightBlue">2.222</td></tr>
<tr><td bgcolor="Goldenrod">3.333</td></tr>
<tr><td> 1.111</td></tr>
</table>
</body>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我可以想到这样的
演示: http://jsfiddle.net/ch6qZ/1/
A新的警告:上面的代码严重依赖于 ES5,因此如果您想使用它,请确保您还在网站上加载了适用于旧浏览器的 ES5-Shim 库。另外,
querySelectorAll
的选择器应该更加具体,最好的情况是您可以为该表提供一个 id。最后,在此示例中,颜色生成是根据Math.random()
进行的,您可能希望自己定义颜色。此示例代码创建一个空对象并将其用作哈希。可用的 td 值作为键名创建一次,每个键的值是共享相同文本内容的 td 数组。完成后,我们循环该哈希并为每个 td 组设置随机背景颜色。
I could think of something like this
Demo: http://jsfiddle.net/ch6qZ/1/
A new words of caution: The above code heavily relies on ES5, so if you want to use it make sure you've also loaded an ES5-Shim library on your site for old'ish browsers. Also, the selector for
querySelectorAll
should be way more specific, best case scenario you can give that table an id. Lastly color generation happens perMath.random()
in this example, you might want to define colors on your own.This example code creates an empty object and uses it as a hash. Available
td
values are created once as keyname and the value for each key is an array oftd
's which share the same text content. After that is done, we loop over that hash and set a random background-color for eachtd
-group.这是一个纯 JavaScript 解决方案(使用 jsFiddle),适用于您发布的示例源:
修改任何(或全部) ) 来更改颜色:
shades
集合columnColorMap
对象darken
函数Here's a pure JavaScript solution (with jsFiddle) that works on your posted sample source:
Modify any (or all) of the following to change the colors:
shades
collectioncolumnColorMap
objectdarken
function你可以使用 Jquery,
you can with Jquery,
假设该值为十进制数,则可以用除以%10的商来提取颜色。
颜色选择功能
Assuming that value is a decimal number, you can extract the color with the quotient divided by % 10.
color choice function