jQuery delete table column

前端 未结 6 594
北荒
北荒 2020-12-17 00:40

I have a table and cannot change markup:

相关标签:
6条回答
  • 2020-12-17 01:18

    If you have the static html (consider table with 10 columns),

    then remove the first column along with header using below:

     $('#table th:nth-child(1),#table td:nth-child(1)').remove();
    

    now the new table will have 9 columns , now you can remove any column using the number:

     $('#table th:nth-child(7),#table td:nth-child(7)').remove();
    
    0 讨论(0)
  • 2020-12-17 01:18

    Remove the first column from each row.

    $('.mytable').find("tr td:nth-child(1)").each(function(){
        $(this).remove()
    });
    
    0 讨论(0)
  • 2020-12-17 01:25

    By Applying some class on targeted column we can remove it. For example

    <tr>
        <td>ID</td>
        <td>Name</td>
        <td><button class="btnRemoveMember">X</button></td>
    </tr>
    

    From above example table we can remove 3rd column of table as follow.

    $(.btnRemoveMember).closest('td').remove();
    
    0 讨论(0)
  • 2020-12-17 01:29

    This works fine for me:

    $(".tableClassName tbody tr").each(function() {
        $(this).find("td:eq(3)").remove();
    });
    
    0 讨论(0)
  • 2020-12-17 01:33

    This uses .delegate() for the handler, and a more native approach using cellIndex to get the cell index that was clicked, and cells to pull the cell from each row.

    Example: http://jsfiddle.net/zZDKg/1/

    $('table').delegate('td,th', 'click', function() {
        var index = this.cellIndex;
        $(this).closest('table').find('tr').each(function() {
            this.removeChild(this.cells[ index ]);
        });
    });
    
    0 讨论(0)
  • 2020-12-17 01:37

    A column is pretty much just cells, so you'll need to manually loop through all the rows and remove the cell by the index.

    This should give you a good starting point for removing the 3rd column:

    $("tr").each(function() {
        $(this).children("td:eq(2)").remove();
    });
    
    0 讨论(0)
提交回复
热议问题