gpt4 book ai didi

jquery - 如何在嵌套表中使用 jQuery .has()

转载 作者:行者123 更新时间:2023-12-01 08:41:59 25 4
gpt4 key购买 nike

我正在尝试为具有“红色”类的嵌套表的 td 单元格着色。我使用的 jQuery 代码将为最上面的表格着色,而不是父 td 单元格。我需要如何修改以下代码。

作为一点引用,我使用的是 SharePoint,它将表嵌套在表中。不幸的是我无法控制这一点。

谢谢

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

最佳答案

我会按照相反的顺序:找到具有类 red 的表,然后将类 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.

关于jquery - 如何在嵌套表中使用 jQuery .has(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45846460/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com