1

我在试图解决这个问题时有点卡住了。我需要获取一些单元格的内容,然后更新此单元格的内部文本

例如,我需要在每一行中获取 class = 2 和 class = 4 的单元格的值,然后更新它们的内部文本

我试图通过这段代码来做到这一点:

$(element).each(function ()
{
   // code code code
});

但这并不是我真正需要的东西,因为这段代码只是在每一行获取每个元素,但我需要一次在每一行获取一些元素

它应该看起来像:

  1. 得到行。
  2. 在当前行获取 class = 1 和 class = 2 的元素
  3. 将他们的内容发送到脚本
  4. 更新单元格内容
  5. 获取下一行
  6. ETC

这是示例表:

<table>
    <tr>
        <td class="1"> Cell One </td>
        <td class="2"> Cell Two </td>
        <td class="3"> Cell Three </td>
        <td class="4"> Cell Four </td>
    </tr>
    <tr>
        <td class="1"> Also one Cell One </td>
        <td class="2"> Also one Cell Two </td>
        <td class="3"> Also one Cell Three </td>
        <td class="4"> Also one Cell Four </td>
    </tr>
</table>

**

并且。.

**

我需要为表中的每一行发送 Ajax 查询,其中包含当前行 ajax.php?id=column1&val=column2 的 2 个单元格的内部文本

4

2 回答 2

2

你可以做类似的事情

另外我认为您正在尝试的是当前 td 是否具有 1 级或 2 级,您可以使用.hasClass()

$('table').find('tr').each(function(){
    var $tr = $(this);
    $tr.find('.1, .2').html(function(idx, html){
        var $td = $(this);

        if($td.hasClass('1')){
            return html + ' class-1';
        } else if($td.hasClass('2')){
            return html + ' class-2';
        }

        return html + ' modified'
    })
})

演示:小提琴

于 2013-07-09T06:34:52.760 回答
1

以下将为您一次性提供一套完整的class="1"内容class="2"

$('tr .1, tr .2')

然后,您可以执行以下操作:

$('tr .1, tr .2').each(function() {
    if($(this).is('.1')) {
       // class="1" stuff
    } else {
       // class="2" stuff
    }
});

如果您更喜欢逐行进行迭代,您可以只选择行,$('tr')然后在迭代中找到感兴趣的子代:

$('tr').each(function() {
    var cols = $('.1, .2', this);
    // do something with cols
});

前进的道路有很多;最好的解决方案将取决于您要达到的目标的细节。

于 2013-07-09T06:33:49.553 回答