jQuery 从 <tr> id 获取 <td> 文本,<td> 是动态生成的,所以我不知道如何或是否有
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1105759/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery get <td> text from <tr> id, <td> is dynamically generated so I don't know how may or if any
提问by Phill Pafford
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"
我已经有一个 jQuery 函数来执行我需要的任务,但是有没有办法循环遍历id="generated_rows"<td>
的特定单元格<tr>
<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>
Need this:
需要这个:
<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);
});
回答by Ricky Supit
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)
你很接近,只需要在你的选择器中使用 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(' '));
});