1

これが私のテーブル形式です。

<table id="table1">
 <tr id="row_1">
  <th>Some Heading</th>
  <td class="t1"></td>
  <td class="t2"></td>
  <td class="t3"></td>
  <td class="t4"></td>
 </tr>
 <tr id="row_2">
  <th>Some Heading</th>
  <td class="t1"></td>
  <td class="t2"></td>
  <td class="t3"></td>
  <td class="t4"></td>
 </tr>
 etc...
</table>

jQuery を使用して個別のテーブル セル (td) の内容を更新するにはどうすればよいですか? #row_1 セル 1 や #row_2 セル 4 などのコンテンツを更新したい場合は、どのように記述すればよいでしょうか。

問題なくデータを更新できる別のテーブルがありますが、それもはるかに単純です。コンテンツの更新が必要な行ごとに 1 つのテーブル セルしかありません。これを使ってやりました。

$('#val_' + i).html("stuff I put in it");

各セルには一意の ID - #val_ + それを識別するいくつかの番号があるため、簡単にループできますが、テーブルが少し複雑なため問題が発生しています。

4

2 に答える 2

1
$('#table1').children('tr').each(function() {

    // where 'x' is the index of the <td> to target
    var $requiredCell = $(this).children('td').eq(x);

    doStuff( $requiredCell );

});

または、単一のセル全体をターゲットにしている場合

$('#row_1 td.t1');

または

$('#row_1 td').first(); // If you can't use the classes...

または

$('#row_1 td').eq(x); // for targeting a td at an arbitrary index 'x'

例(あなたの質問から):

$('#row_1 td').eq(3); // target the fourth <td>. Note index is 0-based
于 2013-02-10T21:10:13.830 に答える
0

すべての tds を取得するには、次のようなものを使用できます。

$("#row_1").find('td').each {function() {
  // do something
});

特定の td を取得するには、次のようにします。

$('#row_1 .t1').something();
于 2013-02-10T21:02:25.873 に答える