jQuery JQuery淡出后删除DOM元素

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/510761/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 09:14:54  来源:igfitidea点击:

JQuery delete DOM element after fading out

jquerydomfadeout

提问by Sebastian Hoitz

I want to delete an DOM element right after fading out. What I did so far is

我想在淡出后立即删除 DOM 元素。到目前为止我所做的是

$(element).click(function()
{
    $(this).fadeOut(500, function() { $().remove(this); });
});

But now I always get this error in Firebug: http://dl.getdropbox.com/u/5912/Jing/2009-02-04_1109.png

但现在我总是在 Firebug 中得到这个错误:http: //dl.getdropbox.com/u/5912/Jing/2009-02-04_1109.png

I guess it is because the fadeOut function is not really done when the callback gets called. And I can not put the $.remove()part after the fadeOut call because otherwise it gets removed instantly.

我想这是因为当回调被调用时,fadeOut 函数并没有真正完成。而且我不能将$.remove()部件放在淡出调用之后,否则它会立即被删除。

So do you know of any way I can do this better?

那么你知道我有什么方法可以做得更好吗?

回答by Georg Sch?lly

You're using the remove()function wrongly.

remove()错误地使用了该功能。

$(element).click(function() {
    $(this).fadeOut(500, function() { $(this).remove(); });
});

回答by Ash

See this earlierSO question.

请参阅此较早的SO 问题。

回答by Shahid Karimi

why messing here just use $('#anydiv').remove();

为什么在这里搞砸只是使用 $('#anydiv').remove();

回答by eyurdakul

or $.remove($(this));

或 $.remove($(this));