admin管理员组文章数量:1429946
I am having the code below and javascript's closure together with anonymous functions are giving me a headache.
for (var i = 0, len = sites.length ; i < len ; i++)
{
$("#thumb"+i).click(function() { $("#shader").show(); $("#thumbInfo"+i).show(); alert("#thumbInfo"+i); });
$("#thumbInfo"+i+" .xbutton").click(function() { $("#shader").hide(); $("#thumbInfo"+i).hide(); });
}
Due to closure, i is always 5 (the sites array has 5 elements), so all the click handlers refer to the same id.
Any workaround?
I am having the code below and javascript's closure together with anonymous functions are giving me a headache.
for (var i = 0, len = sites.length ; i < len ; i++)
{
$("#thumb"+i).click(function() { $("#shader").show(); $("#thumbInfo"+i).show(); alert("#thumbInfo"+i); });
$("#thumbInfo"+i+" .xbutton").click(function() { $("#shader").hide(); $("#thumbInfo"+i).hide(); });
}
Due to closure, i is always 5 (the sites array has 5 elements), so all the click handlers refer to the same id.
Any workaround?
Share Improve this question asked May 1, 2011 at 3:59 George KastrinisGeorge Kastrinis 5,2025 gold badges32 silver badges48 bronze badges 1-
IF you unroll the loop (replace it five times with
i
hardcoded) does it work? I have a feelingi
is not the problem. – Abdullah Jibaly Commented May 1, 2011 at 4:09
4 Answers
Reset to default 6You could always loop with jQuery's each()
.
$.each(sites, function(i) {
$("#thumb"+i).click(function() { $("#shader").show(); $("#thumbInfo"+i).show(); alert("#thumbInfo"+i); });
$("#thumbInfo"+i+" .xbutton").click(function() { $("#shader").hide(); $("#thumbInfo"+i).hide(); });
});
use a closure in your for iteration:
for (var i = 0, len = sites.length ; i < len ; i++)
{
(function(i) {
$("#thumb"+i).click(function() {
$("#shader").show();
$("#thumbInfo"+i).show();
alert("#thumbInfo"+i);
});
$("#thumbInfo"+i+" .xbutton").click(function() {
$("#shader").hide();
$("#thumbInfo"+i).hide();
});
}(i));
}
Place a function returning a function and parameterized by i
outside the loop. JSLint will encourage you to do this anyway, and some might find it more readable to boot.
function make_fn1(i) {
return function() { $("#shader").show(); $("#thumbInfo"+i).show(); };
}
function make_fn2(i) {
return function() { $("#shader").hide(); $("#thumbInfo"+i).hide(); };
}
for (var i = 0; i < sites.length ; i++)
{
$("#thumb"+i).click(make_fn1(i));
$("#thumbInfo"+i+" .xbutton").click(make_fn2(i));
}
However, this could be cleaned up in other ways. For starters, as long as you're using jQuery, $("#shader, #thumbInfo"+i).show();
is more concise. Furthermore, in the current code the notion that the two functions either hide or show the same two elements is not factored out, so could be
function make_fn (i,showhide) {
return function() { $("#shader, #thumbInfo"+i)[showhide]() };
}
for (var i = 0; i < sites.length ; i++)
{
$("#thumb"+i).click(make_fn(i,'show'));
$("#thumbInfo"+i+" .xbutton").click(make_fn(i,'hide'));
}
var len = sites.length ;
for (var i = 0; i < len ; i++)
{
$("#thumb"+i).click(function() { $("#shader").show(); $("#thumbInfo"+i).show(); alert("#thumbInfo"+i); });
$("#thumbInfo"+i+" .xbutton").click(function() { $("#shader").hide(); $("#thumbInfo"+i).hide(); });
}
you might have been assigning 5 to i;
本文标签: jquery and javascript39s closureStack Overflow
版权声明:本文标题:jquery and javascript's closure - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745553945a2663071.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论