简体   繁体   English

在onclick()中更改onclick()

[英]Changing an onclick() inside onclick()

I have a TABLE and each TABLEROW has a div in it with the onclick deleteRow(rowIndex) , where rowIndex is the index of the row that was supposedly clicked on. 我有一个表,每个TABLEROW中都带有一个onclick deleteRow(rowIndex)的div,其中rowIndex是应该被单击的行的索引。 The problem is that any time I try to actively change one of the row's onclick like this, it freaks out and automatically calls it as soon as I change it: 问题是,无论何时我尝试像这样主动更改行的onclick之一,它都会跳出来并在我更改它时自动对其进行调用:

row.onclick = deleteRow(rowIndex);

I am also using Prototype.js. 我也在使用Prototype.js。

Here is my handler: 这是我的处理程序:

deleteRow = function(rowIndex) {
    var table = $('tableElmt');
    table.each(function(row, index) {
        if(index == rowIndex)
            row.onclick = deleteRow(index);
    })
}

When I change onclick like that, it calls deleteRow again instantly and goes into a loop and I cant figure out why. 当我这样改变onclick时,它会立即再次调用deleteRow并进入循环,我无法弄清原因。

I think it has something to do with the way I call deleteRow() inside deleteRow() , although it could also be that I am changing 'onclick' incorrectly. 我认为这与我在deleteRow()调用deleteRow()的方式有关,尽管也可能是我错误地更改了“ onclick”。

I have also tried this to handle clicks on the table rows: 我也尝试过这种方法来处理对表格行的点击:

row.observe('click', deleteRows(rowIndex));

But it does the same thing. 但是它做同样的事情。 Also, this conflicts with the onclick() event handler that already exists for all the rows in the html and I'm not sure how to handle that. 另外,这与html中所有行已经存在的onclick()事件处理程序冲突,我不确定如何处理。

How do I change an onclick() event handler inside that onclick() event handler? 如何在该onclick()事件处理程序中更改onclick()事件处理程序?

You are calling the deleteRow function, and assign it's result to the onclick handler. 您正在调用deleteRow函数,并将其结果分配给onclick处理程序。 What you are trying to achieve is: 您要实现的目标是:

row.onclick = (function(index){
 return function(){
   deleteRow(index);
 }
})(index);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM