jquery如何搜索重复文本并获取其他重复行的行ID。
<tr id='1'>
<td>AA</td>
</tr>
<tr id='2'>
<td>BB</td>
</tr>
<tr id='3'>
<td>AA</td>
</tr>
<tr id='4'>
<td>AA</td>
</tr>
<tr id='5'>
<td>BB</td>
</tr>
<tr id='6'>
<td>CC</td>
</tr>
<tr id='7'>
<td>BB</td>
</tr>
例如我想要id 3,4(对于AA)5,7(对于BB)并且没有CC的ID因为没有重复。 谢谢。
答案 0 :(得分:1)
这样的事情应该有效:
function repeats() {
var seen = {};
var ids = [];
jQuery("tr").each(function() {
var text = jQuery(this).find("td").text();
if(!seen[text]) {
seen[text] = 1;
}
else {
seen[text]++;
}
if(seen[text] > 1) {
ids.push(this.id);
}
});
return ids;
}
答案 1 :(得分:1)
像这样......
$('tr td').each(function(){
var a = $(this).parent().attr('id');
if($(this).text() == 'AA'){
document.write(a + '<br />');
}
});