开发者社区 问答 正文

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

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

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

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

展开
收起
小旋风柴进 2016-03-24 13:50:40 3396 分享
分享
版权
举报
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 举报
    赞同 评论

    评论

    全部评论 (0)

    登录后可评论
问答分类:
问答地址:
AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等