remove() 方法在 animate() 中不起作用

remove() method not working inside animate()

本文关键字:不起作用 animate 方法 remove      更新时间:2023-09-26

在我的网页中,我有一些div显示用户的数据。在某个事件中,我需要删除单击的每个div。我想在删除div 之前让它更漂亮一点。
所以我使用了 animate 方法和 animate 方法的内部回调,我编写了代码以从 dom 中删除该div。 但问题是它表明不能在 null 上调用 delete。动画方法如何在该div 上运行并在其回调中变为 null。
请帮助我解决问题,并建议是否有更好的方法。

    confirmOpen = //Holding reference of div to be removed.
    //animate method is running perfectly fine.
    confirmOpen.animate({"width":"1px","height":"1px"},500,function()
    {
       console.log(confirmOpen); //Logging null
       confirmOpen.remove();   //Showing error that remove method cannot be called on a null value.
    });  

我认为在变量 confirmOpen 中设置引用时存在一些错误。不能删除空值。

confirmOpen

confirmOpen.animate({"width":"1px","height":"1px"},500,function()
    {

将导致错误,因为该函数中没有变量声明,并且您正在删除相同的confirmOpen因此您应该只使用$(this).remove();

confirmOpen = //Holding reference of div to be removed.
    confirmOpen.animate({"width":"1px","height":"1px"},500,function()
    {
       console.log(confirmOpen);
       $(this).remove();   
    }); 

I hope it will work but can you send js fiddle example for more effective results