Remove duplicate 's through jquery

前端 未结 3 439
庸人自扰
庸人自扰 2020-12-01 20:10

I have table which is bound dynamically:


      &l         
相关标签:
3条回答
  • 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?

    0 讨论(0)
  • 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/

    0 讨论(0)
  • 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/

    0 讨论(0)
提交回复
热议问题
test1