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

查看:57
本文介绍了如何恢复使用 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天全站免登陆