Javascript 如何从表中删除列
如何从表中删除列。用于删除列号的文本框值。 有关更多信息,请参阅此链接: Javascript:Javascript 如何从表中删除列,javascript,jquery,jquery-ui,Javascript,Jquery,Jquery Ui,如何从表中删除列。用于删除列号的文本框值。 有关更多信息,请参阅此链接: Javascript: $("#target").click(function () { var removecolunms = $("#textval").val(); $('table colgroup').find('col:eq("' + removecolunms + '")').remove(); $('table tr').find('th:eq("' + removecolunms
$("#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();
});
如果
0
是columnindex,则必须遍历表中的每一行,并删除相应的td和th元素
$('tr').each(function(){
$(this).find('td:nth-child(' + colIndex + ')').remove();
$(this).find('th:nth-child(' + colIndex + ')').remove();
});
退房
fiddle你想要实现什么?您能详细说明一下吗?您的文本中显示了“要删除多少列”,但您使用了:eq()选择器,这实际上是一个迭代,而不是一个数量。只是旁注
$('tr').each(function(){
$(this).find('td:nth-child(' + colIndex + ')').remove();
$(this).find('th:nth-child(' + colIndex + ')').remove();
});
<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>
$('#removeButton').click(
function(){
var numrow = $('#numrow').val();
var numcol = $('#numcol').val();
$('.dataupdate tr:nth-child('+numrow+') td:nth-child('+numcol+')').remove()
});