Geo*_*nis 1 javascript jquery closures
我有下面的代码和javascript的封闭与匿名函数让我头疼.
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(); });
}
Run Code Online (Sandbox Code Playgroud)
由于关闭,我总是5(sites数组有5个元素),所以所有点击处理程序都引用相同的id.
任何解决方法?
你总是可以用jQuery循环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(); });
});
Run Code Online (Sandbox Code Playgroud)