jQuery get< td>来自< tr>的文字id,< td>是动态生成的,所以我不知道怎么可能或者如果有的话

问题描述:

我已经有一个jQuery函数来执行我需要的任务但有一种方法可以遍历特定< td> 单元格>< tr> ,其中id =generated_rows

I have a jQuery function already to perform the task I need but is there a way to loop through the <td> cells of a specific <tr> with the 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>






现在工作以下的功能!,


FUNCTION BELOW NOW WORKS!,

// 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(假设所有数字当然不是固定格式)

You are close, just need to use td instead of tr in your selector. Here's my version to append ".00" at the end of cell's text (assuming all numbers are not already in fixed format of course)

$("#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(' '));
});