【发布时间】:2015-06-12 16:46:16
【问题描述】:
对于 jQuery 中的一系列类似点击事件,我有以下循环。一切正常,除了下面的最后一行带有注释。如何让每个循环迭代分别调用这些函数:step1()、step2()、step3() 等?
for (i = 0; i < 7; i++) {
$("#stepbox" + i).click(function(){
$("#step" + i).show();
$("#stepswrapper section").not("#step" + i).hide();
$("#stepbox" + i).addClass("stepboxactive");
$("#stepboxmain div").not("#stepbox" + i).removeClass("stepboxactive");
step + i(); // I'm stuck here. This doesn't work to create the function calls step1(), step2(), etc.
});
}
【问题讨论】:
-
step1 step2 step3 函数是全局的吗?如果是这样,您可以这样做
window['step' + i]() -
至少,将函数放入一个数组中。但是为什么需要动态调用这样的函数呢?
-
setTimeout("step1()",0);
-
@Legends 将字符串传递给 setTimeout 与调用 eval() 完全相同。
标签: javascript jquery function loops variables