Removing all <tr> from a page using JQuery
I have a table with multiple <tr>
. Some of them has set class on them.
I have <tr>
with no class, with class="Failure"
and with class="Error"
(it's a JUnit html report).
I want to put a button on the page and after click on it to remove all 开发者_Python百科tr with the defined class on them (Failure and Error).
I have tryed something like this:
$("tr").remove(":contains('Failure')");
Thanks
If you mean with class Failure
or Error
, do this:
$("tr.Failure,tr.Error").remove(); // remove those with either
If you mean with both classes:
$("tr.Failure.Error").remove(); // remove those with both
For both of these, you can move the selector to the .remove()
as you had it:
$("tr").remove(".Failure,.Error"); // remove those with either
or:
$("tr").remove(".Failure.Error"); // remove those with both
jQuery makes selecting elements by a class name very easy with element.class-name
syntax. Just select the <tr>
elements with the class you want, and remove them:
$('tr.Failure,tr.Error').remove();
The :contains
selector doesn't match class names, only the text within the element.
Suggest you read up on your jQuery selectors.
Should look like:
$('tr.Failure, tr.Error').remove();
These guys are correct dude, if you wanted it with a button:
$('.button-class').click(function() {
$('tr.Failure, tr.Error').remove();
return false;
});
Also if you wanted to remove the ones without a class:
$('.button-class').click(function() {
$('tr').each(function() {
if ($(this).hasClass('Error') || $(this).hasClass('Failure'))
{
}
else
{
$(this).remove();
}
});
});
('.button-class').click(function()
{
$('tr').each(function()
{
if(!$(this).is('.Failure,.Error'))
$(this).remove();
});
});
精彩评论