如何在嵌套的 table 中使用 jQuery .has()

How to use jQuery .has() within a nested table

我正在尝试使用 class "red" 为具有嵌套 table 的 td 单元格着色。我正在使用的 jQuery 代码将为最顶部的 table 着色,而不是父 td 单元格。我需要如何修改以下代码。

作为一点参考,我使用的 SharePoint 在 table 内嵌套 tables。不幸的是,我无法控制它。

谢谢

$("td:has(.red)").addClass("redBG");

我会倒序:用 class red 找到 table,然后将 class redBG 添加到最近的 td 单元格:

$('table.red').closest('td').addClass('redBG');

这是解决方案,但实际上不是您问题的答案。

对于 jQuery closest method 我们有:

For each element in the set, get the first element that matches the selector by testing the element itself and traversing up through its ancestors in the DOM tree.