jQuery删除表列 [英] jQuery delete table column

查看:84
本文介绍了jQuery删除表列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一张桌子,不能更改标记:

I have a table and cannot change markup:

<table>
    <thead>
        <tr>
            <th>
                blablabla
            </th>
            <th>
                blablabla
            </th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>
                efgd
            </td>
            <td>
                efghdh
            </td>
        </tr>
    </tbody>
</table>

这是我的函数,应删除一列.点击单元格即可调用它:

Here is my function, which should delete a column. It is called on cell click:

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;
}

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

But it deletes something else, not the column I wanted to delete. Where am I wrong?

推荐答案

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

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.

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

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

$("tr").each(function() {
    $(this).filter("td:eq(2)").remove();
});

这篇关于jQuery删除表列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆