如何使用 jQuery 销毁 DOM 元素?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1391793/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to destroy a DOM element with jQuery?
提问by omg
Suppose the jQuery object is $target
.
假设 jQuery 对象是$target
.
回答by Jourkey
回答by Luke
If you want to completely destroythe target, you have a couple of options. First you can remove the object from the DOM as described above...
如果你想完全摧毁目标,你有几个选择。首先,您可以如上所述从 DOM 中删除对象...
console.log($target); // jQuery object
$target.remove(); // remove target from the DOM
console.log($target); // $target still exists
Option 1- Then replace target with an empty jQuery object(jQuery 1.4+)
选项 1- 然后用空的 jQuery 对象替换目标(jQuery 1.4+)
$target = $();
console.log($target); // empty jQuery object
Option 2- Or delete the propertyentirely (will cause an error if you reference it elsewhere)
选项 2- 或者完全删除该属性(如果在其他地方引用它会导致错误)
delete $target;
console.log($target); // error: $target is not defined
More reading: info about empty jQuery object, and info about delete
更多阅读:关于空 jQuery 对象的信息,以及关于删除的信息
回答by Shard
You are looking for the .remove()
function.
您正在寻找该.remove()
功能。