[英]Remove all the 'tr' if the any of the 'td' does not have given text
I have a table with many rows . 我的桌子上有很多行。 first row is the header. 第一行是标题。
i want to delete all the rows if any of its td does not have given text. 我想删除所有行,如果它的任何td没有给定的文本。
<tr>
<td id="1" class="links">Madia</td>
<td id="" class="edit_client" >Press</td>
</tr>
<tr>
<td id="2" class="td_link" >Nagara </td>
<td class="td_link" id="11" class="edit_client">KR Pura</td>
</tr>
I want to delete all the tr , if any of its td does not have given text say "me hussy". 我想删除所有tr,如果它的任何td没有给出文字说“ me hussy”。
$('tr').each(function () {
});
i do not want delete first row because its header. 我不想删除第一行,因为它的标题。 so function should check from second row onwards. 因此功能应从第二行开始进行检查。
Try doing this 试试这个
$('table tr:gt(0)').each(function() {
if ($('td:contains("me hussy")', this).length == 0) $(this).remove();
});
Demo on jsFiddle.net jsFiddle.net上的演示
EDIT: 编辑:
Thanks mesiesta for :gt(0)
感谢meiesiesta :gt(0)
EDIT 编辑
With respect to OP's comment 关于OP的评论
var name = "me hussy";
$('table tr:gt(0)').each(function() {
if ($('td:contains("'+ name +'")', this).length == 0) $(this).remove();
});
var name="me hussy";
$('tr').not(':first').not($('tr').find('td:contains('+name+')').parent()).remove()
尝试这个
$('tr:not(:contains("me hussy")):not(:first)').remove();
You should use thead
and tbody
to separate the rows but this should work; 您应该使用thead
和tbody
分隔行,但这应该可行。
$(function() {
var $table = $('table#byID');
var $bodyRows = $table.find('tr').not(':first'); // Exclude the first row
var hasEmptyTds = $bodyRows.find('td').filter(function() { // Filter by text, could also use the :contains selector
return ($(this).text() != 'me hussy')
}).length > 0;
if (hasEmptyTds) {
$bodyRows.remove(); // Remove all table body rows
}
});
If you need to re-use the functionality, wrap it in a function, otherwise you can just replace the mustMatchString
in the jQuery line below. 如果您需要重复使用该功能,请将其包装在一个函数中,否则只需替换下面的jQuery行中的mustMatchString
。 To remove the first item, use the jQuery .slice
method. 要删除第一项,请使用jQuery .slice
方法。
function runRemoval(mustMatchString) {
jQuery('tr').slice(1).each(function() {
if(!jQuery('td:contains("' + mustMatchString + '")', this).length) {
jQuery(this).remove();
}
});
}
runRemoval('me hussy');
http://jsfiddle.net/steveukx/D6Hkh/ http://jsfiddle.net/steveukx/D6Hkh/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.