admin管理员组文章数量:1334370
Is there a way i can pass information into a nested function ? The problem is i want to use jQuery to animate an object being removed and then after have it remove the object from the dom. But theres no way to pass information into the nested function. I first though the bellow would work but no luck,
tab = this.tab //this.tab is a dom element
$(this.tab).effect('drop',null,null, function(tab)
{
$(tab).remove()
})
People have suggested that i store the element in a global put this function is part of a class and there can be many objects which may call this function at the same time.
Thankyou!
Is there a way i can pass information into a nested function ? The problem is i want to use jQuery to animate an object being removed and then after have it remove the object from the dom. But theres no way to pass information into the nested function. I first though the bellow would work but no luck,
tab = this.tab //this.tab is a dom element
$(this.tab).effect('drop',null,null, function(tab)
{
$(tab).remove()
})
People have suggested that i store the element in a global put this function is part of a class and there can be many objects which may call this function at the same time.
Thankyou!
Share Improve this question asked Jul 21, 2011 at 10:03 kjones1876kjones1876 7622 gold badges10 silver badges17 bronze badges 1-
Doesn't
this
inside the closure refer to the object being animated? jQuery is quite clever in setting context, tryconsole.log
gingthis
inside the closure. – nikc Commented Jul 21, 2011 at 10:14
1 Answer
Reset to default 9Using closures you should be able to simply do
var tab = this.tab
$(tab).effect('drop', null, null, function() { $(tab).remove(); });
Note that tab
is defined outside the "nested function", but since JavaScript supports closures, the function can access variables defined in the same scope as itself. In other words, it'll be able to access tab
.
Also note that it's not this.tab
, since this
refers to the context in which the code is called.
Addendum: I'm not a jQuery-guy (weird, I know), but I'd imagine that the function you pass to effect()
will be executed in the tab
element's context (i.e. inside the function this
refers to the tab
element). If so, you could probably just do
$(this.tab).effect('drop', null, null, function() { $(this).remove(); });
本文标签: javascriptA way to pass a variable into a nested functionStack Overflow
版权声明:本文标题:javascript - A way to pass a variable into a nested function - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742345911a2457516.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论