【问题标题】:jQuery get <td> text from <tr> id, <td> is dynamically generated so I don't know how may or if anyjQuery 从 <tr> id 获取 <td> 文本,<td> 是动态生成的,所以我不知道如何或是否有
【发布时间】:2009-07-09 18:39:52
【问题描述】:

我已经有一个 jQuery 函数来执行我需要的任务,但是有没有办法通过 id="generated_rows" 遍历特定 &lt;tr&gt;&lt;td&gt; 单元格

<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);
});

【问题讨论】:

    标签: jquery html selector


    【解决方案1】:

    你很接近,只需要在你的选择器中使用 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(' '));
    });
    

    【讨论】:

    • 你能用表ID代替行ID吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-05
    • 1970-01-01
    • 1970-01-01
    • 2016-01-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多