Jou*_*key 190

$target.remove();你在找什么?

http://docs.jquery.com/Manipulation/remove

  • 当然,这只会将其从可见文档中删除.如果有来自JavaScript的节点的其他引用,例如$ target变量本身,该对象现在将转义垃圾收集器.如果你想破坏它,你也必须失去对它的所有引用.我不太清楚为什么你想要DESTROY一个DOM元素.也许你只是讨厌$ target.可怜的目标,它对你做了什么? (74认同)
  • 当他独自一人时,$ target是善良的,但当他在他的100,000个克隆朋友附近时,他变得讨厌. (51认同)
  • @SaurabhNanda - Empty将删除对象的内容,但不会删除(或销毁)对象本身. (3认同)
  • `.empty()`会有类似的效果吗? (2认同)

Luk*_*uke 46

如果你想完全摧毁目标,你有几个选择.首先,您可以如上所述从DOM中删除对象...

console.log($target);   // jQuery object
$target.remove();       // remove target from the DOM
console.log($target);   // $target still exists
Run Code Online (Sandbox Code Playgroud)

选项1 - 然后用空的jQuery对象替换target (jQuery 1.4+)

$target = $();
console.log($target);   // empty jQuery object
Run Code Online (Sandbox Code Playgroud)

选项2 - 或完全删除属性(如果您在别处引用它将导致错误)

delete $target;
console.log($target);   // error: $target is not defined
Run Code Online (Sandbox Code Playgroud)

更多阅读:有关空jQuery对象的信息,以及有关删除的信息

  • 为什么`删除$ target`不起作用:http://perfectionkills.com/understanding-delete/#misconceptions为什么`$ target = null`不起作用? (3认同)

Sha*_*ard 13

您正在寻找这个.remove()功能.

http://docs.jquery.com/Manipulation/remove