gpt4 book ai didi

javascript - 在匹配的文本上应用 CSS

转载 作者:行者123 更新时间:2023-11-29 10:13:26 25 4
gpt4 key购买 nike

我正在尝试应用一个文本与 sibling 元素匹配的类。

我的特定条件是:

I have a table with multiple rows based on data that I get through database. One of the td elements has my defined class. Now I wanted to apply a class only on those td elements where the text of this element matches with another one.

所以它就像是 html/text 相等的 td 有那个类。

我试过了:

$('#table tbody>tr').find('td[class^="customtd"]').each(function() {
if($(this).html().trim() == $(this).siblings('td').html().trim()) {
$(this).addClass('active');
}else {
$(this).removeClass('active');
}
});

最佳答案

您必须迭代每个兄弟 td(或使用 filter),检查文本匹配,然后添加类:

$('#table tbody>tr').find('td[class^="customtd"]').each(function() {
var text = $(this).text();
$(this).siblings("td").filter(function() {
return $(this).text() == text;
}).addClass("active");
});

关于javascript - 在匹配的文本上应用 CSS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28090375/

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