如何恢复用jQuery删除的元素? [英] How to Restore an Element Removed with jQuery?

查看:601
本文介绍了如何恢复用jQuery删除的元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果使用删除元素

$('.notification').remove();

我们如何重新创建它.

推荐答案

您无法获取该特定实例.使用$.remove()会将其从DOM中删除.您可以创建它的副本,在DOM中移动它,隐藏它,等等.根据您的项目要求,您可能还有许多其他选择.

You can't get that particular instance back. Using $.remove() removes it from the DOM. You can create a clone of it, move it around the DOM, hide it, etc., though. Depending on what your project requires, you likely have many other options.

如果您对该特定实例不太感兴趣,则可以创建一个新实例.假设那是一个带语句的div:

If you're not too interested in that particular instance, you can create a new one. Suppose that was a div with a statement:

$("<div />").addClass("notification").text("I exist!").appendTo("body");

如果要保留该特定元素的副本,可以$.clone()并删除原始元素:

If you want to keep a copy of that particular element around, you can $.clone() it and remove the original:

var clone = $(".notification").clone(); // making zeh' clones!
$(".notification").remove();            // original is gone
$("body").append(clone);                // appears to have returned

这篇关于如何恢复用jQuery删除的元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆