admin管理员组文章数量:1353245
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.
<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').each(function () {
});
i do not want delete first row because its header. so function should check from second row onwards.
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.
<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').each(function () {
});
i do not want delete first row because its header. so function should check from second row onwards.
Share Improve this question edited Feb 24, 2012 at 7:28 Amar Palsapure 9,6661 gold badge29 silver badges46 bronze badges asked Feb 24, 2012 at 7:19 HussyHussy 2,0294 gold badges25 silver badges32 bronze badges 2- similar question stackoverflow./questions/9412995/… – Anil D Commented Feb 24, 2012 at 9:01
- For your new query you can post new question. – Amar Palsapure Commented Feb 24, 2012 at 12:18
5 Answers
Reset to default 5Try doing this
$('table tr:gt(0)').each(function() {
if ($('td:contains("me hussy")', this).length == 0) $(this).remove();
});
Demo on jsFiddle
EDIT:
Thanks mesiesta for :gt(0)
EDIT
With respect to OP's ment
var name = "me hussy";
$('table tr:gt(0)').each(function() {
if ($('td:contains("'+ name +'")', this).length == 0) $(this).remove();
});
try this
$('tr:not(:contains("me hussy")):not(:first)').remove();
var name="me hussy";
$('tr').not(':first').not($('tr').find('td:contains('+name+')').parent()).remove()
You should use thead
and tbody
to separate the rows but this should work;
$(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. To remove the first item, use the jQuery .slice
method.
function runRemoval(mustMatchString) {
jQuery('tr').slice(1).each(function() {
if(!jQuery('td:contains("' + mustMatchString + '")', this).length) {
jQuery(this).remove();
}
});
}
runRemoval('me hussy');
http://jsfiddle/steveukx/D6Hkh/
本文标签: javascriptRemove all the 39tr39 if the any of the 39td39 does not have given textStack Overflow
版权声明:本文标题:javascript - Remove all the 'tr' if the any of the 'td' does not have given text - Stack Overflo 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743929034a2563499.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论