我已经有一个 jQuery 函数来执行我需要的任务,但是有没有办法循环遍历 <td>
特定细胞 <tr>
使用 id="generated_rows"
<table>
<tr id="generated_rows">
<td class="row_class" id="row_id_1">text 1</td>
<td class="row_class" id="row_id_2">text 2</td>
<td class="row_class" id="row_id_3">text 3</td>
<td class="row_class" id="row_id_4">text 4</td>
<td class="row_class" id="row_id_5">text 5</td>
</tr>
</table>
需要这个:
<table>
<tr id="generated_rows">
<td class="row_class" id="row_id_1">text 1.00</td>
<td class="row_class" id="row_id_2">text 2.00</td>
<td class="row_class" id="row_id_3">text 3.00</td>
<td class="row_class" id="row_id_4">text 4.00</td>
<td class="row_class" id="row_id_5">text 5.00</td>
</tr>
</table>
下面的功能现在可以使用了!
// Check for whole numbers and append .00
$('#generated_rows td.row_class').each(function() {
var x = Number($(this).text()).toFixed(2);
$(this).text(x);
});
最佳答案
你很接近,只需要在你的选择器中使用 td 而不是 tr 。这是我在单元格文本末尾附加“.00”的版本(当然假设所有数字都不是固定格式)
$("#generated_rows > td.row_class").each(function() {
var $this = $(this);
var splitText = $this.text().split(' ');
splitText[1] = Number(splitText[1]).toFixed(2);
$this.text(splitText.join(' '));
});
关于jQuery 从 <tr> id 获取 <td> 文本,<td> 是动态生成的,所以我不知道如何可能或是否有,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1105759/