Firefox Javascript事件匿名函数

Gui*_*ips 1 javascript firefox closures anonymous-function event-handling

我正在尝试在用户单击HTML表格中的单元格时注册匿名函数.这是一些原始的,纯粹的代码:

document.getElementById(
    "course"+displayed_year_index+occurrences_indices[displayed_year_index]).onclick =
        eval("function() {PrintReceipt("+result.years[result_year_index].rul_code+");};");
Run Code Online (Sandbox Code Playgroud)

注意使用eval,因为它位于一个循环中,并且匿名函数每次都是不同的.

可以说,这在Firefox 2中运行得非常好.但是,Firefox 3会抛出"语法错误",指向"函数"一词后的括号内.

有没有人对如何解决这个问题有任何明智的想法?


为了清楚地说明我正在尝试做什么,这里有一个简单的例子:

for (index=0; index<4; index++) {
    document.getElementById("div"+index).onclick = 
        eval("function () {Foo(index);};");
}
Run Code Online (Sandbox Code Playgroud)

换句话说,我希望触发相同的功能,每个参数值都有不同的参数值div.

Tom*_*Tom 5

你尝试过这样的事吗?

document.getElementById('course' + displayed_year_index + occurences_indices[displayed_year_index]) =
    function (nr)
    {
        return function () { PrintReceipt(nr) }
    } (result.years[result_year_index].rul_code);
Run Code Online (Sandbox Code Playgroud)

您能否发布循环以帮助我们找到问题,而不是让我们猜测您正在尝试做什么?