jQuery remove HTML table column

后端 未结 11 1389
粉色の甜心
粉色の甜心 2020-12-13 07:13

I have a HTML table like this:

DE
11条回答
  •  天涯浪人
    2020-12-13 08:18

    When I've read this post I tried the first solution using jQuery's remove function. But it seems to have a problem with this function when using it on a table row to delete cell. The problem is bind to a concurrent modification. In the exemple with this reponse if you try to use the index() function it will not work because cell index is changing each time you remove a cell. One solution could be to use the hide() function on the cell you want to delete. But if you really need to delete the column (remove it from the DOM) the way which has worked for me were to use the javascript native to remove the column.

    $(function() {      
        $('table tr').each(function(e, row) {
        var i = 0;
        $(row).find('td, th').each(function(e, cell) {          
            if (i == 1)  { 
               row.removeChild(cell);  
            }   
            i++;                    
        });
    });
    

    In this example you delete the second column of the table : i == 1 ...

提交回复
热议问题