如何从桌子上删除列

问题描述 投票:0回答:4
如何从表中删除列。删除列号的文本框值。 请参阅此链接以获取更多信息: JavaScript:

$("#target").click(function () { var removecolunms = $("#textval").val(); $('table colgroup').find('col:eq("' + removecolunms + '")').remove(); $('table tr').find('th:eq("' + removecolunms + '")').remove(); $('table tr').find('td:eq("' + removecolunms + '")').remove(); });

jsfiddle

javascript jquery jquery-ui
4个回答
0
投票
$('table').find('tr').each(function() { this.removeChild(this.cells[ 0 ]); });
where

0

是columnIndex


0
投票
$('tr').each(function(){ $(this).find('td:nth-child(' + colIndex + ')').remove(); $(this).find('th:nth-child(' + colIndex + ')').remove(); });

检查Fiddle

    

<table border="1" style="width:100%" class="dataupdate"> <tr> <td>Row 1 Col1</td> <td>Row 1 Col2</td> <td>Row 1 Col3</td> <td>Row 1 Col4</td> <td>Row 1 Col5</td> <td>Row 1 Col6</td> </tr> <tr> <td>Row 2 Col1</td> <td>Row 2 Col2</td> <td>Row 2 Col3</td> <td>Row 2 Col4</td> <td>Row 2 Col5</td> <td>Row 2 Col6</td> </tr> <tr> <td>Row 3 Col1</td> <td>Row 3 Col2</td> <td>Row 3 Col3</td> <td>Row 3 Col4</td> <td>Row 3 Col5</td> <td>Row 3 Col6</td> </tr> </table> <form> <input type="text" id="numrow" placeholder="Row" > <input type="text" id="numcol" placeholder="Column"> <input type="button" id="removeButton" value="Remove" > </form>


0
投票
  $('#removeButton').click(
  function(){
   var numrow = $('#numrow').val();
   var numcol = $('#numcol').val();  
$('.dataupdate tr:nth-child('+numrow+') td:nth-child('+numcol+')').remove()
  });
Fiddle

Http://jsfiddle.net/yy7qmedt/1/

    

jQuery免费解决方案:


0
投票

function deleteColumn(tableEl, colIndex) { for (let row of tableEl.rows) { row.deleteCell(colIndex) } }

<table id="table"> <tr> <th><button id="btn" onclick="deleteColumn(table, 0)"><code>deleteColumn(table, 0)</code></button></th> <th><button id="btn" onclick="deleteColumn(table, 1)"><code>deleteColumn(table, 1)</code></button></th> </tr> <tr> <td>Foo</td> <td>42</td> </tr> <tr> <td>Bar</td> <td>666</td> </tr> </table>
    
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.