到目前为止我有这个,但它不会改变单元格值:
function setCellValue(tableId, rowId, colNum, newValue)
{
$('#'+tableId).find('tr#'+rowId).find('td:eq(colNum)').html(newValue);
};
最佳答案
通过连接索引创建选择器(在 :eq()
中,索引从 0
开始)。尽管您需要对行选择器执行相同的操作,因为 rowId
是 tr
的索引,而不是 id
。
function setCellValue(tableId, rowId, colNum, newValue)
{
$('#'+tableId).find('tr:eq(' + (rowId - 1) + ')').find('td:eq(' + (colNum - 1) + ')').html(newValue);
};
或使用 :nth-child()
伪类选择器。
function setCellValue(tableId, rowId, colNum, newValue)
{
$('#'+tableId).find('tr:nth-child(' + rowId + ')').find('td:nth-child(' + colNum + ')').html(newValue);
};
或者通过避免 find()
使用单个选择器 方法。
function setCellValue(tableId, rowId, colNum, newValue)
{
$('#' + tableId + ' tr:nth-child(' + rowId + ') td:nth-child(' + colNum + ')').html(newValue);
// or
$('#' + tableId + ' tr:eq(' + (rowId - 1) + ') td:eq(' + (colNum - 1) + ')').html(newValue);
};
关于javascript - 如何按行和列索引设置单元格的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38876193/