How to tell which row number is clicked in a table?

前端 未结 6 511
陌清茗
陌清茗 2020-12-01 03:28

I have a table like the following:

6条回答
  •  一整个雨季
    2020-12-01 03:59

    A simple and jQuery free solution:

    document.querySelector('#elitable').onclick = function(ev) {
       // ev.target <== td element
       // ev.target.parentElement <== tr
       var index = ev.target.parentElement.rowIndex;
    }
    

    Bonus: It works even if rows are added/removed dynamically

提交回复
热议问题