This question already has an answer here:
这个问题已经有了答案:
- Finding the location of a TD in a table 5 answers
- 在表5中找到TD的位置
I'm trying to create an onclick function that returns the row and the column of the clicked cell from a table in JavaScript without using jQuery. What I have so far will return the column, but I can't figure out how to make it return the row.
我正在尝试创建一个onclick函数,该函数在不使用jQuery的情况下,从JavaScript表中返回被单击单元格的行和列。到目前为止我所拥有的将返回列,但我不知道如何使它返回行。
**cellToAppend.addEventListener("click", clicked);**
**function clicked() {
alert("clicked cell at: " + (this).cellIndex + ", ");
}**
1 个解决方案
#1
6
Use parentNode
to get the row.
使用parentNode获取行。
function clicked() {
alert("clicked cell at: " + this.cellIndex + ", " + this.parentNode.rowIndex);
}
#1
6
Use parentNode
to get the row.
使用parentNode获取行。
function clicked() {
alert("clicked cell at: " + this.cellIndex + ", " + this.parentNode.rowIndex);
}