【问题标题】:remove() method not working inside animate()remove() 方法在 animate() 中不起作用
【发布时间】:2013-09-09 05:40:35
【问题描述】:

在我的网页中,我有某些显示用户数据的 div。在某个事件中,我需要删除每个单击的 div。我想在删除 div 之前让它更漂亮一点。
所以我使用了 animate 方法和 animate 方法的内部回调,我编写了代码以从 dom 中删除该 div。 但问题是它表明无法在 null 上调用 remove。 animate 方法是如何在该 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.
    });  

【问题讨论】:

  • 我可以有任何 jsfiddle 参考吗?
  • 你试过像var confirmOpen =一样声明confirmOpen吗?您正在使用可以在其他地方设置为 null 的全局变量?
  • 你可以使用(this).remove();
  • your code 有效!!。检查这个fiddle。您应该将您分配的值放入 confirmOpen 以获得更清晰的信息。
  • 我刚刚测试了你的代码,没问题。我的猜测是您在闭包中使用此代码。独立,它可以工作,但将其放在其他代码中,无法访问“confirmOpen”。当然,这是一个猜测,没有看到所有代码。使用 $(this).remove();

标签: javascript jquery html css jquery-animate


【解决方案1】:

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

【讨论】:

  • 但他说他可以制作动画。这怎么可能?
  • Animate 方法运行良好。
  • 试试 $(this).remove();
  • 非常抱歉浪费了我们的时间......我在另一种方法中将变量设置为空。所以在动画结束之前,它被设置为空。该死的你星期一早上......谢谢大家的帮助。
【解决方案2】:

confirmOpen 里面

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

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

【讨论】:

    【解决方案3】:
    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-04-05
      • 1970-01-01
      • 1970-01-01
      • 2018-08-04
      • 1970-01-01
      • 2012-10-07
      • 2011-11-19
      • 2014-07-02
      相关资源
      最近更新 更多