Restore Multiple Detached Elements in JQuery

女生的网名这么多〃 提交于 2019-12-02 04:54:01

问题


I have a link inside of a table that, when clicked, removes the entire parent tr. I am using detach() for the purposes of restoring that item later based on an event.

Typically, one would store this as a variable, then recall it later and have it append() later, but what if I need to restore multiple rows?

There is no .= method to add more to a variable is there?

JSFiddle = http://jsfiddle.net/nErDy/


回答1:


Why not use an array?

var deleted = [];
//Allow people to delete rows
$('a.delete').click(function() {
    deleted.push($(this).parent().parent().detach());
});

//Restore all
$('a.restore').click(function() {
    $.each(deleted, function(i, v) {
        $('#teams').append(v);
    });
});​

http://jsfiddle.net/wirey00/nErDy/2/




回答2:


you should use an array to store all the dettached items: http://jsfiddle.net/nErDy/1/




回答3:


What everyone else said, use an array. Also, move the css for odd rows into an actual css, so it respects the deleted rows.

#teams tr:nth-child(even) td { background-color : #cecece }​

http://jsfiddle.net/44Qab/



来源:https://stackoverflow.com/questions/12058896/restore-multiple-detached-elements-in-jquery

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!