【发布时间】:2021-03-06 11:44:31
【问题描述】:
我需要根据用户在表格中的点击返回行和单元格索引。
我的功能:
function updatePrice()
{
var rIndex, cellIndex, table = document.getElementsByTagName('table')[0];
for(var i = 0; i < table.rows.length; i++){
table.rows[i].onclick = function valorLinha() {
rIndex = this.rowIndex;
/*console.log here works fine*/
};
for (var j = 0; j < table.rows[i].cells.length; j++)
table.rows[i].cells[j].onclick = function () {
cellIndex = this.cellIndex;
};
/*I NEED values of rIndex and cellIndex here*/
console.log(rIndex);
console.log(cellIndex);
}
}
console.log 运行时我变得不确定。
我该如何解决这个问题?
【问题讨论】:
-
I NEED values of rIndex and cellIndex here不,你没有。唯一需要它们的地方(以及拥有它们的地方)是在点击处理函数内部。 jsfiddle.net/5ork1x0h -
好的,谢谢回复。我用这段代码解决了我的问题:morioh.com/p/aae77200692f
标签: javascript