我需要将所有单元格的颜色更改为红色,其标题为" Severity" (不使用列索引):
<table>
<tbody>
<tr>
</tr>
<tr>
<th title="System.String">Name</th>
<th title="System.String">Value</th>
<th title="LinqTest.CvssSeverity">Severity</th>
</tr>
<tr>
<td>AccessVector</td>
<td>RemoteNetwork</td>
<td>High</td>
</tr>
<tr>
<td>AccessComplexity</td>
<td>High</td>
<td>Low</td>
</tr>
<tr>
<td>AuthenticationRequired</td>
<td>None</td>
<td>High</td>
</tr>
<tr>
<td>ConfidentialityImpact</td>
<td>Partial</td>
<td>Medium</td>
</tr>
<tr>
<td>IntegrityImpact</td>
<td>Partial</td>
<td>Medium</td>
</tr>
<tr>
<td>AvailabilityImpact</td>
<td>Partial</td>
<td>Medium</td>
</tr>
</tbody>
</table>
<table>
<tbody>
<tr>
</tr>
<tr>
<th title="System.String">Name</th>
<th title="System.String">Value</th>
<th title="System.String">SomeOtherCol</th>
<th title="LinqTest.CvssSeverity">Severity</th>
</tr>
<tr>
<td>AccessVector</td>
<td>RemoteNetwork</td>
<td>1234</td>
<td>High</td>
</tr>
<tr>
<td>AccessComplexity</td>
<td>High</td>
<td>High</td>
<td>Low</td>
</tr>
<tr>
<td>AuthenticationRequired</td>
<td>None</td>
<td>1234</td>
<td>High</td>
</tr>
<tr>
<td>ConfidentialityImpact</td>
<td>Partial</td>
<td>1234</td>
<td>Medium</td>
</tr>
<tr>
<td>IntegrityImpact</td>
<td>Partial</td>
<td>1234</td>
<td>Medium</td>
</tr>
<tr>
<td>AvailabilityImpact</td>
<td>Partial</td>
<td>1234</td>
<td>Medium</td>
</tr>
</tbody>
</table>
请注意,这应该适用于页面上具有Severity列的所有表格(表格上可能有Id,但它们是自动生成的,因此我无法使用ID。)
实现这一目标的好方法是什么?
这是对null解决方案的一个小修改,它也适用于嵌套表(如果它可以帮助某人......):
$("th:contains('Severity')").parent().closest('table').each(function () {
var idx = $(this).find("th:contains('Severity')").index() + 1;
$(this).find("td:nth-child(" + idx + "):contains('High')").css("color", "red");
});
答案 0 :(得分:3)
不是很漂亮,但这是相当适应的:
// Get the index of the 'Severity' table header and add one (as index() begins at 0)
var idx = $("table th:contains('Severity')").index() + 1;
// Using nth-child in combination with that index (as to target the correct column)
// Colour each cell which contains the text 'High' in red.
$("table td:nth-child("+ idx +"):contains('High')").css("color", "red");
关于您的评论,这适用于包含“严重性”标题的所有表格:
$("table").each(function(){
var idx = $(this).find("th:contains('Severity')").index() + 1;
$(this).find("td:nth-child("+ idx +"):contains('High')").css("color", "red");
});
答案 1 :(得分:0)
$("td:nth-child(3):contains('High')").css("background-color", "red");
以上代码将解决您的问题。我已经更新了上面提到的小提琴代码。链接如下