gpt4 book ai didi

javascript - 每次我单击表格单元格时,我想在每次单击时console.log每个单元格的值?正确的呼召是什么?

转载 作者:行者123 更新时间:2023-12-02 20:51:53 26 4
gpt4 key购买 nike

        <!-- The Row Number 0 -->

<th>First Name</th>
<th>Last Name</th>
<th>Age</th>

<!-- End Row Number 0 -->
var anArray = [
["A1", "B1", "C1"],
["A2", "B2", "C2"],
["A3", "B3", "C3"],
["A4", "B4", "C4"],
["A5", "B5", "C5"],
["A1", "B1", "C1"],
["A2", "B2", "C2"],
["A3", "B3", "C3"],
["A4", "B4", "C4"],
["A5", "B5", "C5"]
];


var tables = document.getElementById("table");

var i, j;


for (i = 1; i < anArray.length; i++) {
// create a new row
var newRow = table.insertRow(tables.length);


for (j = 0; j < anArray[i].length; j++) {
// create a new cell
cell = newRow.insertCell(j);

// add value to the cell
cell.innerHTML = anArray[i][j];

tables.rows[i].cells[j].onclick = function() {

// var result = tables.rows[i].cells.item(j).innerHTML;
rIndex = this.parentElement.rowIndex + 1;
cIndex = this.cellIndex;
var result = tables.rows[rIndex].cells[cIndex].item().innerHTML; // this line of code that I was confused.
console.log("Row : " + rIndex + " , Cell : " + cIndex);
console.log(result);
}
}
}

我很困惑如何返回多维数组的值?每次我单击表格单元格时,我想在每次单击时console.log每个单元格的值?正确的呼召是什么?有什么建议吗?

最佳答案

你的问题我也有点不确定。我可以给你的最简单的答案或建议是可能在每个单元格都有一个函数调用,有几种方法可以实现它。看看这里:

Using an HTML button to call a JavaScript function

您可以将 input 标记替换为 tr 标记。

关于javascript - 每次我单击表格单元格时,我想在每次单击时console.log每个单元格的值?正确的呼召是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61586753/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com