在 jQuery 中突出显示 td 中特定内容的行

发布于 2024-12-14 02:40:35 字数 265 浏览 4 评论 0原文

到目前为止,我可以让我的代码突出显示特定的 td,但更好的是突出显示整行。有谁知道该怎么做?

var mySearch = 'No';
$('table tbody tr td:contains("' + mySearch + '")').filter
(function(){
if($.trim($(this).text()) == mySearch)
    $(this).addClass("prequal-status-n");       
}); 

So far I can get my code to highlite the specific td, but even better would be to highlite the entire row. Does anyone know how to do this?

var mySearch = 'No';
$('table tbody tr td:contains("' + mySearch + '")').filter
(function(){
if($.trim($(this).text()) == mySearch)
    $(this).addClass("prequal-status-n");       
}); 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

骑趴 2024-12-21 02:40:35

通过将 $(this).addClass("prequal-status-n") 更改为 $(this).closest('tr').addClass("prequal-status-n") 您可以选择整行,而不仅仅是行中的一个单元格。

这是使用 .closest() 选择单元格的 tr 父标记的 jsfiddle: http://jsfiddle.net/jasper/LhbUG/

注意:.closest('tr') 可以替换为 .parent( ) 作为td 标记的直接父级是 tr 标记。请记住,使用 .parent() 会将您的 $(this) 选择器限制为 tr 标记的直接后代。

这是给你的一些文档:

By changing $(this).addClass("prequal-status-n") to $(this).closest('tr').addClass("prequal-status-n") you can select the entire row rather than just a cell within the row.

Here is a jsfiddle of using .closest() to select the tr parent tag of a cell: http://jsfiddle.net/jasper/LhbUG/

Note: .closest('tr') can be replaced by .parent() as the direct parent of the td tag is a tr tag. Just keep in mind that using .parent() restricts your $(this) selector to being a direct descendant of the tr tag.

Here's some documentation for ya:

庆幸我还是我 2024-12-21 02:40:35

您可以使用 closparentest() 并找到 tr

var mySearch = 'No';
$('table tbody tr td:contains("' + mySearch + '")').filter
(function(){
if($.trim($(this).text()) == mySearch)
    $(this).parent().addClass("prequal-status-n");       
})

You could use closparentest() and find the tr

var mySearch = 'No';
$('table tbody tr td:contains("' + mySearch + '")').filter
(function(){
if($.trim($(this).text()) == mySearch)
    $(this).parent().addClass("prequal-status-n");       
})
长不大的小祸害 2024-12-21 02:40:35

尝试

var mySearch = 'No';
$('table tbody tr td:contains("' + mySearch + '")')
                   .parent("tr").addClass("prequal-status-n");

Try

var mySearch = 'No';
$('table tbody tr td:contains("' + mySearch + '")')
                   .parent("tr").addClass("prequal-status-n");
谁的新欢旧爱 2024-12-21 02:40:35

您想要使用 closest 方法来选择 包含

$(this).closest("tr").addClass(...);

来自 closest 的 API 文档:

获取与选择器匹配的第一个元素,从当前元素开始并在 DOM 树中向上移动。

You want to use the closest method to select the <tr> that contains the <td>:

$(this).closest("tr").addClass(...);

From the API documentation for closest:

Get the first element that matches the selector, beginning at the current element and progressing up through the DOM tree.

我不吻晚风 2024-12-21 02:40:35

尝试这样的事情:

$("td:contains('" + mySearch + "')").closest("tr").toggleClass("prequal-status-n");

Try something like this:

$("td:contains('" + mySearch + "')").closest("tr").toggleClass("prequal-status-n");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文