jQuery获取< td>来自< tr>的文字id,< td>是动态生成的,所以我不知道如何可能或如果有 [英] jQuery get <td> text from <tr> id, <td> is dynamicly generated so I don't know how may or if any

查看:83
本文介绍了jQuery获取< td>来自< tr>的文字id,< td>是动态生成的,所以我不知道如何可能或如果有的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,我已经有了一个jQuery函数来执行我需要的任务,但是有没有一种方法可以通过id =generated_rows特定的单元格来循环

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

// 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代替在你的选择器中。这里是我的版本,在单元格文本的末尾添加.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(' '));
});

这篇关于jQuery获取&lt; td&gt;来自&lt; tr&gt;的文字id,&lt; td&gt;是动态生成的,所以我不知道如何可能或如果有的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆