开发者社区> 问答> 正文

如何恢复已删除的元素到他原来的位置

用jQuery可以复制并删除一个元素:

var $temp = $("#test").clone(true);
$("#test").remove();

这时$temp已经不在dom tree之中了,如果想把它加回到dom tree,可以$("body").append($temp);。但是如何能把这个$temp加到他原来的位置呢?难道只能提前记录他的父亲元素么?

展开
收起
小旋风柴进 2016-03-24 13:50:40 3296 0
1 条回答
写回答
取消 提交回答
  • 决定这么做: 先记录每个元素的位置:

    var $locations = []; $(".auto-header").each(function() { var $this = $(this), offset = $this.offset(); $locations.push($this.siblings().eq($this.index())); }); 删除并恢复元素:

    $(".auto-header").each(function(i, e) { $te = $(this).clone(true); $(this).remove(); // remove // do something $locations[i].before($te); // restore });

    2020-03-26 23:08:26
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载