请参阅fiddle 。当我单击单元格时,我可以获得列索引和行索引。我想知道如何使用这些索引来改变表格的颜色?以下是 JavaScript 代码:
$('#example tbody').on( 'click', 'td', function () {
alert('Row ' + $(this).closest("tr").index());
alert('Column ' + $(this).closest("td").index());
});
我必须使用索引而不是$(this).color()
的原因是我必须保存索引才能更改另一个表的单元格颜色 .
最佳答案
修改你的JS代码如下
$(document).ready(function() {
$("table tbody tr td").on("click", function() {
$(this).closest("table").find("td").css({
backgroundColor: "white"
});
$(this).css({
backgroundColor: "red"
});
});
});
我已经创建了一个CodePen,请查看。
https://codepen.io/trgiangvp3/full/wqrwao/
关于javascript - 如何通过行和列的索引更改表格单元格的颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45663453/