javascript - 如何使用 jquery 删除 HTML TABLE 中的最后一列?

标签 javascript jquery html-table

我有一个 HTML 表格:

<table id="persons" border="1">
    <thead id="theadID">
        <tr>
            <th>Name</th>
            <th>sex</th>
            <th>Message</th>
        </tr>
    </thead>
    <tbody id="tbodyID">
        <tr>
            <td>Viktor</td>
            <td>Male</td>
            <td>etc</td>
        </tr>
        <tr>
            <td>Melissa</td>
            <td>Female</td>
            <td>etc</td>
        </tr>
        <tr>
            <td>Joe</td>
            <td>Male</td>
            <td>etc</td>
        </tr>
    </tbody>
</table>
<input type="button" onclick="deleteLastColumn();" value="do it"/>

我需要一个 javascript/jquery 代码,它删除表中的最后一列(消息):

function deleteLastColumn() {
    $("#theadID tr th:not(:last-child)......
    $("#tbodyID tr td:not(:last-child)......
}

所以结果应该是这样的:

<table id="persons" border="1">
    <thead id="theadID">
        <tr>
            <th>Name</th>
            <th>sex</th>
        </tr>
    </thead>
    <tbody id="tbodyID">
        <tr>
            <td>Viktor</td>
            <td>Male</td>
        </tr>
        <tr>
            <td>Melissa</td>
            <td>Female</td>
        </tr>
        <tr>
            <td>Joe</td>
            <td>Male</td>
        </tr>
    </tbody>
</table>

我知道有 ":not(last)"方法,但我找不到任何示例来解决我的问题。 谁能帮帮我?

最佳答案

尝试

$('#persons tr').find('th:last-child, td:last-child').remove()

演示:Fiddle

关于javascript - 如何使用 jquery 删除 HTML TABLE 中的最后一列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17972130/

相关文章:

javascript - AJAX post方法,有时在页面加载时触发,有时不触发

javascript - 正则表达式:了解音节计数器代码

javascript - 无需托管的 Firebase 云消息传递(Web/JavaScript)

jQuery 用动画改变 DIV 背景

html - html 表格中等高缩放单元格

javascript - AWS Lambda 持续时间与函数运行时间

javascript - 使用JQueryUI作为音频搜索控件,并在更改歌曲时将新的 slider 重新绑定(bind)到相同的<audio>控件

javascript - 选择 li 项时更改无序列表的值

css - 如何将文本居中到文本区域?

html如何制作一个有多个表头的表格?