gpt4 book ai didi

Javascript 和 HTML : get the first value of an array row

转载 作者:行者123 更新时间:2023-11-30 10:03:01 24 4
gpt4 key购买 nike

我有这个 Javascript 函数,无论我点击我的 HTML 数组,它都会“提醒”被点击单元格的内容:

var table = document.getElementById("tableID");
if (table != null) {
for (var i = 0; i < table.rows.length; i++) {
for (var j = 0; j < table.rows[i].cells.length; j++)
table.rows[i].cells[j].onclick = function () {tableText(this);};
}
}
function tableText(tableCell) {alert(tableCell.innerHTML);}

我的目标是只返回被点击行的第一个单元格的值(我试图管理它,但我是 Javascript 的新手......)。提前致谢!

最佳答案

你也可以使用 jquery

$("#tableID tbody tr").click(function() {
var first_td = $(this).closest("tr") // Finds the closest <tr>
.find("td:first") // Finds the first <td>
.text(); // Gets its text.
console.log("Selected first_td is : ", first_td);
});

关于Javascript 和 HTML : get the first value of an array row,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30596174/

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