2017-06-14 59 views
-2

我有一个表格和一个函数。 我可以采取单元格的参数。如何获得当前单元格的索引

我是新来的JS。请帮助找到当前单元格的索引。谢谢

function changePosition(currentCell) { 
 
    let top = currentCell.offsetTop 
 
    
 
    //need to find index of cell 
 
}
<table class="square-table"> 
 
      <tr> 
 
      <td onmouseover = 
 
     "changePosition(this)"></td> 
 
      <td onmouseover = 
 
     "changePosition(this)"></td> 
 
      <td onmouseover = 
 
     "changePosition(this)"></td> 
 
      <td onmouseover = function() { 
 
     "changePosition(this)"></td>    
 
      </tr> 
 
</table>

+0

我不明白这个问题。我会看到这不是一个有效的HTML表格。 – Goose

+0

'function(){ changePosition(this)' 需要关闭'}' – fxlacroix

+0

可能重复[获取内联onclick类元素的索引 - 纯js没有jQuery](https://stackoverflow.com/questions/) 44185632 /获取-指数的级元件与 - 直列的onclick纯-JS-NO-jquery的)。请参阅[我的回答](https://stackoverflow.com/a/44185902/1541563),其中提供了避免使用'onmouseover'属性的错误做法的方法。在表上使用'.addEventListener('mouseover',...)'来委派来自每个'​​'的事件。 –

回答

1

我发现这里的解决方案

link

function changePosition(currentCell) { 
 
    let top = currentCell.offsetTop 
 
    currentCell.cellIndex 
 
    
 
    //need to find index of cell 
 
}

我proble m已解决。对不起

相关问题