【发布时间】: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(); -
我刚刚测试了你的代码,没问题。我的猜测是您在闭包中使用此代码。独立,它可以工作,但将其放在其他代码中,无法访问“confirmOpen”。当然,这是一个猜测,没有看到所有代码。使用 $(this).remove();
标签: javascript jquery html css jquery-animate