在 jQuery 中突出显示 td 中特定内容的行
到目前为止,我可以让我的代码突出显示特定的 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技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
通过将
$(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
标记的直接后代。这是给你的一些文档:
.parent()
: http://api.jquery.com /parent.closest()
: http://api.jquery.com /最近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 thetr
parent tag of a cell: http://jsfiddle.net/jasper/LhbUG/Note:
.closest('tr')
can be replaced by.parent()
as the direct parent of thetd
tag is atr
tag. Just keep in mind that using.parent()
restricts your$(this)
selector to being a direct descendant of thetr
tag.Here's some documentation for ya:
.parent()
: http://api.jquery.com/parent.closest()
: http://api.jquery.com/closest您可以使用 closparentest() 并找到 tr
You could use closparentest() and find the tr
尝试
Try
您想要使用 closest 方法来选择
包含
:
来自
closest
的 API 文档:You want to use the closest method to select the
<tr>
that contains the<td>
:From the API documentation for
closest
:尝试这样的事情:
Try something like this: