jquery重复文本搜索和剪切

时间:2011-09-29 01:34:13

标签: jquery

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因为没有重复。 谢谢。

2 个答案:

答案 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;
}

Fiddle here

答案 1 :(得分:1)

像这样......

$('tr td').each(function(){
    var a = $(this).parent().attr('id');
    if($(this).text() == 'AA'){
        document.write(a + '<br />');
    }
});

示例: http://jsfiddle.net/jasongennaro/GDEWm/1/