Javascript 如何删除表中的td

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31468519/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 06:44:15  来源:igfitidea点击:

How to remove td in table

javascriptjqueryhtmlasp.net-mvc-4

提问by Lam Son

How can I remove a tdcell in a table with MVC4?

如何td使用MVC4删除表格中的单元格?

Could I use JQuery or JavaScript? And if so, how?

我可以使用 JQuery 或 JavaScript 吗?如果是这样,如何?

<table class="table table-striped table-bordered table-hover" id="tblParticipantList">
    <thead>
        <tr>
            <th>Name</th>
            <th>Adress</th>
            <th>Redeem Code</th>
        </tr>
    </thead>
    <tbody>
        <tr data-id="columnId" id="customerPartial_94">
            <td data-field="NAME">Attn Donna</td>
            <td data-field="ADDRESS">3046 Lavon Drive Newyork America 7504</td>
            <td data-field="SECURITY_REDEMPTION_CD"></td>
        </tr>
        <tr data-id="columnId" id="customerPartial_95">
            <td data-field="NAME">F 1 La 1</td>
            <td data-field="ADDRESS">Asd 1 s 1 Ci 1 s</td>
            <td data-field="SECURITY_REDEMPTION_CD"></td>
        </tr>
    </tbody>
</table>

回答by Luc

To remove TD item, you should know exactly td what you want to remove.

要删除 TD 项目,您应该确切地知道要删除的内容。

  1. Remove All TD item

    $('#tblParticipantList > tr > td').remove();
    
  2. Remove TD at specified Row

    $('#tblParticipantList > tr').eq(rowNum).children('td').remove();
    
  3. Remove TD at specified Row and Column

    $('#tblParticipantList > tr').eq(rowNum).children('td').eq(colNum).remove();
    
  1. 删除所有 TD 项目

    $('#tblParticipantList > tr > td').remove();
    
  2. 删除指定行的 TD

    $('#tblParticipantList > tr').eq(rowNum).children('td').remove();
    
  3. 删除指定行和列的 TD

    $('#tblParticipantList > tr').eq(rowNum).children('td').eq(colNum).remove();
    

回答by RRK

Remove child tdof the position you want using eq(). Use proper id and class in the selector for expected result.

使用 删除td您想要的位置的子级eq()。在选择器中使用正确的 id 和 class 以获得预期结果。

$('.table-striped tr').each(function(){
    $(this).children('td').eq(3).remove();
});