繁体   English   中英

删除元素的父行

[英]Remove element's parent row

我有一个在单元格中有按钮的表,如下所示:

<table>
    <tr>
        <td>information</td>
        <td>
            <button onclick="function(id, this)">Text</button>
        </td>
    </tr>
</table>

按下按钮时调用的函数会执行一些ajax操作,如果成功,则应从DOM中删除按钮所在的整行。 (这是一个删除功能;)

但是,我似乎无法让jQuery删除正确的行。 我用过$('#id').parent().parent().remove(); ,我认为它会去:按钮 - >单元格 - >行,但它只是删除表的第一行?! 哪里输了? :(

http://jsfiddle.net/ZnX5J/

<table>
    <tr>
        <td>information</td>
        <td>
            <button onclick="removeRow(this)">Text</button>
        </td>
    </tr>
    <tr>
        <td>blah</td>
        <td>
            <button onclick="removeRow(this)">Text</button>
        </td>
    </tr>
</table>

JavaScript的

removeRow = function(el) {
    $(el).parents("tr").remove()       
}

.closest()可以很容易地做到 -

$(this).closest('tr').remove();

您可以删除内联onClick并像这样处理点击 -

$('button').on('click',function(){
   $(this).closest('tr').remove();
});

演示------> http://jsfiddle.net/2nJYe/

工作演示http://jsfiddle.net/cse_tushar/6mMkM/

$('.b1').click(function () {
    $(this).parent().parent().remove();
});

你做得对,但没有身份证,如果你这样做,它会像你期望的那样工作,

$(this).parent().parent().remove();

但在这种情况下建议使用.closest()。

$(this).closest("tr").remove();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM