这是我的表格格式。
<table id="table1">
<tr id="row_1">
<th>Some Heading</th>
<td class="t1"></td>
<td class="t2"></td>
<td class="t3"></td>
<td class="t4"></td>
</tr>
<tr id="row_2">
<th>Some Heading</th>
<td class="t1"></td>
<td class="t2"></td>
<td class="t3"></td>
<td class="t4"></td>
</tr>
etc...
</table>
如何使用 jQuery 更新单独表格单元格 (td) 的内容?更好的表达方式是,如果我想更新 #row_1 cell 1 或 #row_2 cel 4 等的内容,我该如何编写它?
我有另一个表,我可以毫无问题地更新数据,但它也简单得多。它每行只有一个表格单元格需要更新内容。我用这个做到了。
$('#val_' + i).html("stuff I put in it");
每个单元格都有一个唯一的 ID - #val_ + 一些标识它的数字,这样我就可以轻松地循环它,但稍微复杂的表格给我带来了麻烦。
最佳答案
$('#table1').children('tr').each(function() {
// where 'x' is the index of the <td> to target
var $requiredCell = $(this).children('td').eq(x);
doStuff( $requiredCell );
});
或者,如果您只是针对整个单个单元格
$('#row_1 td.t1');
或者
$('#row_1 td').first(); // If you can't use the classes...
或者
$('#row_1 td').eq(x); // for targeting a td at an arbitrary index 'x'
例如(来自你的问题):
$('#row_1 td').eq(3); // target the fourth <td>. Note index is 0-based
关于javascript - 使用/jQuery 更新表格单元格数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14802514/