Firefox Javascript事件匿名函数

Firefox Javascript事件匿名函数,javascript,firefox,event-handling,closures,anonymous-function,Javascript,Firefox,Event Handling,Closures,Anonymous Function,当用户单击HTML表中的单元格时,我试图注册一个匿名函数。以下是一些未经处理的原始代码: document.getElementById( "course"+displayed_year_index+occurrences_indices[displayed_year_index]).onclick = eval("function() {PrintReceipt("+result.years[result_year_index].rul_code+");};"); 注

当用户单击HTML表中的单元格时,我试图注册一个匿名函数。以下是一些未经处理的原始代码:

document.getElementById(
    "course"+displayed_year_index+occurrences_indices[displayed_year_index]).onclick =
        eval("function() {PrintReceipt("+result.years[result_year_index].rul_code+");};");
注意eval的使用,因为它位于一个循环中,并且匿名函数每次都不同

可以说,这在Firefox2中工作得非常好。但是,Firefox3抛出了一个“语法错误”,指向单词“function”后面的括号内

有人对我如何解决这个问题有什么聪明的想法吗

为了清楚地说明我想做什么,这里有一个非常简单的示例:

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

换句话说,我希望为每个div触发具有不同参数值的相同函数。

您尝试过类似的方法吗

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

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

您尝试过类似的方法吗

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

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

这似乎是您想要做的方向:

document.getElementById("course"+displayed_year_index+occurrences_indices[displayed_year_index]).addeventlistener("click",  function() {
    var current_rul_code = result.years[result_year_index].rul_code;
    PrintReceipt(current_rul_code);
}, true);

这将导致在循环的每个迭代中在不同的范围内创建每个to-onclick事件。剩下的就交给你了。

看来这就是你想要走的方向:

document.getElementById("course"+displayed_year_index+occurrences_indices[displayed_year_index]).addeventlistener("click",  function() {
    var current_rul_code = result.years[result_year_index].rul_code;
    PrintReceipt(current_rul_code);
}, true);

这将导致在循环的每个迭代中在不同的范围内创建每个to-onclick事件。将处理其余部分。

使用Tom建议的闭包


下面是John Resig的一个很好的解释:使用Tom建议的闭包


下面是John Resig的一个很好的解释:在这种情况下不应该使用IMHO闭包,并且没有必要为每个onlick创建一个新函数。onlick使用的内存远远超过了需要的内存,eval是错误的答案

您知道使用getElementById获得的元素是一个对象,并且可以为其赋值吗

for ( /* your definition */ ) {
  var e = document.getElementById(
    "course"+displayed_year_index+occurrences_indices[displayed_year_index]
  );
  e.rul_code = result.years[result_year_index].rul_code;
  e.onclick = PrintReceipt;
}
但您应该首先定义打印收据:

function PrintReceipt() {
  //This function is called as an onclick handler, and "this" is a reference to the element that was clicked.
  if (this.rul_code === undefined) { return; }
  //Do what you want with this.rul_code
  alert (this.rul_code);
}

在这种情况下,不应使用IMHO闭包,也不需要为每个onlick创建新函数。onlick使用的内存远远超过了需要的内存,eval是错误的答案

您知道使用getElementById获得的元素是一个对象,并且可以为其赋值吗

for ( /* your definition */ ) {
  var e = document.getElementById(
    "course"+displayed_year_index+occurrences_indices[displayed_year_index]
  );
  e.rul_code = result.years[result_year_index].rul_code;
  e.onclick = PrintReceipt;
}
但您应该首先定义打印收据:

function PrintReceipt() {
  //This function is called as an onclick handler, and "this" is a reference to the element that was clicked.
  if (this.rul_code === undefined) { return; }
  //Do what you want with this.rul_code
  alert (this.rul_code);
}

答案很好,但是链接到一个关于闭包的好解释会有很大帮助。我理解你的代码,但它仍然让我害怕;不知道什么是结束的人就没有机会了。不幸的是,我还没有在网上任何地方看到关于闭包的清晰、简洁的解释:不幸的是,我需要分配给onclick事件,因此我无法将参数传递给匿名函数。答案很好,但链接到闭包的良好解释将非常有帮助。我理解你的代码,但它仍然让我害怕;不知道什么是结束的人就没有机会了。不幸的是,我还没有在网上看到关于闭包的清晰、简洁的解释:不幸的是,我需要分配给onclick事件,因此我无法将参数传递给匿名函数。这不起作用,因为PrintReceive的参数在onclick调用时需要是常量。它不需要是常量,因为当事件激发时,函数将在其原始范围内调用。谢谢,我已经尝试了前面提到的示例,但它不起作用。这是因为从onClick调用函数时,result.years[result\u year\u index].rul\u代码不在范围内。实际上我需要的是变量13208的值,在这种情况下,它将被传递给函数。这不起作用,因为当onclick调用PrintReceive的参数时,它需要是一个常量。它不需要是一个常量,因为当事件激发时,函数将在其原始范围内被调用。谢谢,如前所述,我试过你的例子,但不起作用。这是因为从onClick调用函数时,result.years[result\u year\u index].rul\u代码不在范围内。我实际上需要的是变量13208的值,在这种情况下,它将被传递给函数。千万不要用大锤敲开螺母,谢谢。我一直在绞尽脑汁想找到答案!你当然是完全正确的。千万不要用大锤敲开螺母,谢谢。我一直在绞尽脑汁想找到答案!