Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/joomla/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
jQuery删除表列_Jquery - Fatal编程技术网

jQuery删除表列

jQuery删除表列,jquery,Jquery,我有一个表,无法更改标记: 喋喋不休 喋喋不休 efgd efghdh 这是我的函数,它应该删除一列。在单元格单击时调用: function (menuItem, menu) { var colnum = $(this).prevAll("td").length; $(this).closest('table').find('thead tr th:eq('+colnum+')').remove(); $(this).closest("table").find('t

我有一个表,无法更改标记:


喋喋不休
喋喋不休
efgd
efghdh
这是我的函数,它应该删除一列。在单元格单击时调用:

function (menuItem, menu) {
    var colnum = $(this).prevAll("td").length;

    $(this).closest('table').find('thead tr th:eq('+colnum+')').remove();
    $(this).closest("table").find('tbody tr td:eq('+colnum+')').remove();    
    return;
}

但它删除了其他内容,而不是我想删除的列。我错在哪里?

列几乎只是单元格,因此您需要手动循环所有行并按索引删除单元格

这将为您删除第3列提供一个良好的起点:

$("tr").each(function() {
    $(this).children("td:eq(2)").remove();
});
这对处理程序使用了
.delegate()
,并且使用了一种更为自然的方法,使用
cellIndex
获取单击的单元格索引,并使用
cells
从每一行提取单元格

$('.mytable').find("tr td:nth-child(1)").each(function(){
    $(this).remove()
});
示例:


这对我来说很好:

$(".tableClassName tbody tr").each(function() {
    $(this).find("td:eq(3)").remove();
});

从每行中删除第一列

$('.mytable').find("tr td:nth-child(1)").each(function(){
    $(this).remove()
});

如果您有静态html(考虑有10列的表)

然后使用以下命令删除第一列和标题:

 $('#table th:nth-child(1),#table td:nth-child(1)').remove();
现在,新表将有9列,现在您可以使用以下数字删除任何列:

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

通过在目标列上应用一些类,我们可以删除它。 比如说

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

我看不出你的意图,请说得更具体些。你想删除全部还是只删除特定的列?这不太管用
filter()
应用于
tr
元素,而不是
td
元素,因此这不会删除任何内容。相反,您需要执行
$(this).children().filter()
或更好的
$(this.find()
)。
$(.btnRemoveMember).closest('td').remove();