javascript - 如何按行和列索引设置单元格的值?

标签 javascript jquery

到目前为止我有这个,但它不会改变单元格值:

function setCellValue(tableId, rowId, colNum, newValue)
{
    $('#'+tableId).find('tr#'+rowId).find('td:eq(colNum)').html(newValue);
};

最佳答案

通过连接索引创建选择器( :eq() 中,索引从 0 开始)。尽管您需要对行选择器执行相同的操作,因为 rowIdtr 的索引,而不是 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/

相关文章:

javascript - 直接 float 在内容旁边的粘性侧边栏,由 js 插入

javascript - AngularJS - 在事件中刷新 ngRepeat 数组数据

javascript - AngularJS 观察 ngForm 引用数组中 $valid 的变化

即使在更新 DIV 后,Javascript 函数仍会继续执行

jquery 滚动 : start position

javascript - 在 JavaScript 中调用函数

javascript - JW Player 6 插件在 Firefox 中不工作 - Drupal

javascript - 我如何优化基于 asp.NET MVC 的 Web 应用程序的性能

jquery - IOS 8.1 Safari :$. ajax 给出超时错误 dom 异常 23

jquery - 如何使用 JavaScript 显式显示 jQuery DatePicker