I have table which is bound dynamically:
test1
|
|
&l
相关标签:
-
2020-12-01 20:41
Try -
var seen = {};
$('table tr').each(function() {
var txt = $(this).text();
if (seen[txt])
$(this).remove();
else
seen[txt] = true;
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table id="test">
<tr>
<td>test1
<td>
</tr>
<tr>
<td>test2
<td>
</tr>
<tr>
<td>test1
<td>
</tr>
<tr>
<td>test2
<td>
</tr>
</table>
Code is taken (and very slightly changed) from this question - JQuery: Remove duplicate elements?
-
2020-12-01 20:49
Here you go:
$('#btnRearrange').bind("click", function() {
$("#test td").each(function () {
var tdText = $(this).text();
$("#test td")
.filter(function () {
return tdText == $(this).text();
})
.not(":first")
.remove();
});
});
Try it out here:
http://jsfiddle.net/MAnLM/3/
-
2020-12-01 20:50
$('#btnRearrange').bind("click", function() {
var contents = {}, text;
$("#test td").each(function() {
text = $(this).text();
if( !( text in contents ) ) {
contents[text] = true;
}
else {
$( this.parentNode ).remove();
}
});
});
jsfiddle: http://jsfiddle.net/jKs4k/