【发布时间】:2010-09-22 16:16:48
【问题描述】:
当用户单击 HTML 表格中的单元格时,我正在尝试注册一个匿名函数。下面是一些原始的、纯粹的代码:
document.getElementById(
"course"+displayed_year_index+occurrences_indices[displayed_year_index]).onclick =
eval("function() {PrintReceipt("+result.years[result_year_index].rul_code+");};");
注意eval 的使用,因为它位于一个循环中,并且匿名函数每次都不同。
可以这么说,这在 Firefox 2 中工作得非常好。但是,Firefox 3 会抛出一个“语法错误”,指向“函数”一词之后的括号内。
有人对我如何解决这个问题有任何聪明的想法吗?
为了清楚地说明我想要做什么,这里有一个非常简化的示例:
for (index=0; index<4; index++) {
document.getElementById("div"+index).onclick =
eval("function () {Foo(index);};");
}
换句话说,我希望为每个div触发具有不同参数值的相同函数。
【问题讨论】:
标签: javascript firefox event-handling closures anonymous-function