[英]jQuery - find table row containing table cell containing specific text
I need to get a tr
element which contains a td
element which contains specific text.我需要获取一个
tr
元素,其中包含一个td
元素,该元素包含特定的文本。 The td
will contain that text and only that text (so I need text = 'foo'
not text contains 'foo'
logic). td
将包含该文本并且仅包含该文本(所以我需要text = 'foo'
而不是text contains 'foo'
逻辑)。
So I need the equivalent of the following 'pseudo jQuery':所以我需要相当于下面的“伪 jQuery”:
var tableRow = $(table td[text = 'foo']).parent('tr');
Can anyone provide the correct syntax?谁能提供正确的语法?
I know this is an old post but I thought I could share an alternative [not as robust, but simpler] approach to searching for a string in a table.我知道这是一篇旧帖子,但我想我可以分享一种替代方法 [不是那么强大,而是更简单] 来搜索表中的字符串。
$("tr:contains(needle)");
//where needle is the text you are searching for. //其中 needle 是您要搜索的文本。
For example, if you are searching for the text 'box', that would be:例如,如果您正在搜索文本“框”,则为:
$("tr:contains('box')");
This would return all the elements with this text.这将返回带有此文本的所有元素。 Additional criteria could be used to narrow it down if it returns multiple elements
如果它返回多个元素,则可以使用其他标准来缩小范围
$(function(){
var search = 'foo';
$("table tr td").filter(function() {
return $(this).text() == search;
}).parent('tr').css('color','red');
});
Will turn the text red for rows which have a cell whose text is 'foo'.对于具有文本为“foo”的单元格的行,会将文本变为红色。
This will search text in all the td's inside each tr and show/hide tr's based on search text这将在每个 tr 内的所有 td 中搜索文本,并根据搜索文本显示/隐藏 tr
$.each($(".table tbody").find("tr"), function () {
if ($(this).text().toLowerCase().replace(/\s+/g, '').indexOf(searchText.replace(/\s+/g, '').toLowerCase()) == -1)
$(this).hide();
else
$(this).show();
});
<input type="text" id="text" name="search">
<table id="table_data">
<tr class="listR"><td>PHP</td></tr>
<tr class="listR"><td>MySql</td></tr>
<tr class="listR"><td>AJAX</td></tr>
<tr class="listR"><td>jQuery</td></tr>
<tr class="listR"><td>JavaScript</td></tr>
<tr class="listR"><td>HTML</td></tr>
<tr class="listR"><td>CSS</td></tr>
<tr class="listR"><td>CSS3</td></tr>
</table>
$("#textbox").on('keyup',function(){
var f = $(this).val();
$("#table_data tr.listR").each(function(){
if ($(this).text().search(new RegExp(f, "i")) < 0) {
$(this).fadeOut();
} else {
$(this).show();
}
});
});
Demo You can perform by search() method with use RegExp matching text 演示您可以通过 search() 方法使用 RegExp 匹配文本执行
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.