计算html表中的唯一值

时间:2018-04-26 08:49:38

标签: javascript jquery html html-table

我想在html表中计算唯一值。(RGB颜色)

我正在使用第三方网站,该网站使用PHP在表格中写入值。我无法访问PHP脚本。

PHP在这里写了“{colorcode}”我定义的rgb-to-hex。我有5个十六进制值:
火:#FF8C00
医疗帮助:#FD0202
有害物质:#19070B
其他:#4876FF
技术援助:#0000FF

我的目标是,我可以单独计算每种颜色并将其写在另一张表中 这是我的网站,显示表格:https://www.feuerwehr-forstern.de/einsaetze/
我想要计算的表。

<table>
<tr style="font-size:16px; background-color:#670200; color:#FFFFFF;">
<th><b>Nr.</b></th>
<th><b>Missionstart</b></th>
<th><b>Title</b></th>
<th><b>Kind of mission</b></th>
<th><b>Place</b></th>
<th></th>
</tr>{liststart}
<tr>
<td style="color:#FFFFFF;" bgcolor={colorcode}><b>{missionnr}</b></td>
<td>{startdate} {starttime}</td>
<td>{missiontitle}</td>
<td>{kind of mission}</td>
<td>{missionplace}</td>
<td><u>{linkreport}</u></td>
</tr>{listend}
</table>

其他表,我想在“:”之后写下计数结果。

<table>
<tr style="font-size:16px; color:#FFFFFF;">
<th style="background-color:#FF8C00;"><b>fire:</b></th>
<th style="background-color:#FD0202;"><b>medical help:</b></th>
<th style="background-color:#19070B;"><b>hazardous materials:</b></th>
<th style="background-color:#4876FF;"><b>other:</b></th>
<th style="background-color:#0000FF;"><b>technical assistance:</b></th>
</tr>
</table>

3 个答案:

答案 0 :(得分:2)

您可以尝试以下操作:var blue_count = $('[bgcolor=#0000FF]').length获取td元素的计数,其中bgcolor属性的值为#0000FF。然后你可以把计数值附加到你想要的地方。

但这只是你解决它的想法......不是最好的方式......

祝你好运

答案 1 :(得分:0)

这是新的代码。

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script type="text/javascript">
$(function() {
var orange_count = $('[bgcolor=#ff8c00]').size()
$(".important1").text("Brand: " + orange_count);
});
</script>
<script type="text/javascript">
$(function() {
var red_count = $('[bgcolor=#fd0202]').size()
$(".important2").text("First Responder: " + red_count);
});
</script>
<script type="text/javascript">
$(function() {
var black_count = $('[bgcolor=#19070b]').size()
$(".important3").text("Gefahrstoffe: " + black_count);
});
</script>
<script type="text/javascript">
$(function() {
var royalblue_count = $('[bgcolor=#4876ff]').size()
$(".important4").text("Sonstige: " + royalblue_count);
});
</script>
<script type="text/javascript">
$(function() {
var blue_count = $('[bgcolor=#0000FF]').size()
$(".important5").text("Technische Hilfeleistung: " + blue_count);
});
</script>
<table>
<tr style="font-size: 16px; color: #ffffff;">
<th style="background-color: #ff8c00;"><b class="important1">Brand</b></th>
<th style="background-color: #fd0202;"><b class="important2">First Responder</b></th>
<th style="background-color: #19070b;"><b class="important3">Gefahrstoffe</b></th>
<th style="background-color: #4876ff;"><b class="important4">Sonstige</b></th>
<th style="background-color: #0000ff;"><b class="important5">Technische Hilfeleistung</b></th>
</tr>
</table>

答案 2 :(得分:0)

我查看了您的代码以及您在问题中提供的链接, 在链接中,颜色代码都是大写的,如下所示:bgcolor="#4876FF"
所以你不能用这样的小写选择器来获取它们:$('[bgcolor=#4876ff]').size()
你应该先解决这个问题。然后,在每个页面中,您只需要检查document.ready事件一次。所以其中一个将完成这项工作:

$(function() {

});

只需在其中一个块中编写代码即可。
祝你好运......

相关问题