我有一个表,显示从数据库中获取的数据。我已经在 PHP 中做到了这一点,例如 $data_array()
;
我想要的是找到具有相同电子邮件地址的记录并用红色突出显示这些行。这意味着,如果某个电子邮件地址在表中重复两次或三次,则所有 3 行都会突出显示为红色。
我是这样做的:
$exist = array();
foreach($data_array as $da){
if(in_array($da['Email'], $exist)){
$row_color = "#f00"; // Red
}else{
$row_color = "#fff"; // White
}
echo "<tr style='color:".$row_color.";'>";
echo "<td>".$da['Email']."</td>";
echo "</tr>";
$exist[] = $da['Email'];
}
以上代码有效并突出显示第二行和第三行,但不显示第一行。显然我的代码没有任何内容可以突出显示具有重复值的第一条记录。
我该怎么做?也可以用 PHP 或 Jquery 完成。有什么帮助吗??
最佳答案
在 jQuery
,您可以使用filter()
并使用includes()
检查是否 <td>
的文本在数组中。
var emails = ['<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="debbb3bfb7b2ef9ebcb2bff0bdb1b3" rel="noreferrer noopener nofollow">[email protected]</a>', '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="bccfd9d0d9dfc8fcded0dd92dfd3d1" rel="noreferrer noopener nofollow">[email protected]</a>'];
$('table td').filter(function() {
return emails.includes($(this).text().trim());
}).css('backgroundColor', 'red');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
<tr>
<th>Company</th>
</tr>
<tr>
<td><a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="5c39313d35306d1c3e303d723f3331" rel="noreferrer noopener nofollow">[email protected]</a></td>
</tr>
<tr>
<td><a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="04776168616770446668652a676b69" rel="noreferrer noopener nofollow">[email protected]</a></td>
</tr>
<tr>
<td><a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="3a5e5574554e695f565f594e7a58565b14595557" rel="noreferrer noopener nofollow">[email protected]</a></td>
</tr>
<tr>
<td><a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d4b1b9b5bdb8e594b6b8b5fab7bbb9" rel="noreferrer noopener nofollow">[email protected]</a></td>
</tr>
<tr>
<td><a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="83f0e6efe6e0f7c3e1efe2ade0ecee" rel="noreferrer noopener nofollow">[email protected]</a></td>
</tr>
<tr>
<td><a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="52363d1c3d2601373e373126063d3d12303e337c313d3f" rel="noreferrer noopener nofollow">[email protected]</a></td>
</tr>
<tr>
<td><a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="0d7e626068427965687f48606c64614d6f616c236e6260" rel="noreferrer noopener nofollow">[email protected]</a></td>
</tr>
<tr>
<td><a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="54273138313720143638357a373b39" rel="noreferrer noopener nofollow">[email protected]</a></td>
</tr>
</table>
关于php - Jquery 或 PHP 根据重复值突出显示表行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50274093/