且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

jQuery删除表行

更新时间:2023-12-04 12:04:58

该行将被删除,但是随着单击使您跟随链接,刷新页面后会立即将其恢复.

The row is deleted but as clicking makes you follow the link, it's immediately restored when the page is refreshed.

在回调的末尾添加return false; event.preventDefault(); ,以防止出现默认行为:>

Add return false; or event.preventDefault(); at the end of the callback to prevent the default behavior :

$(document).ready(function() {
    $("#favoriteFoodTable .deleteLink").on("click",function() {
        var tr = $(this).closest('tr');
        tr.css("background-color","#FF3700");
        tr.fadeOut(400, function(){
            tr.remove();
        });
        return false;
    });
});

演示

请注意,我使用closest编写了更可靠的代码:如果在两者之间插入另一个元素,则仍会找到tr.

Note that I used closest for a more reliable code : if another element comes in between, the tr will still be found.