0

テーブル id="myTable" の各行の各セルを反復処理し、セルの子 div の ID を取得したいと考えています。

これまでのところ、次のようなものがあります。

$("td.theCell").each(function() {
  var id = $(this).attr('id'); //this would give me the td id but I want its child div's id
});

私のhtmlは次のようなものです:

<table id='myTable'>
  <tr>foo</tr>
  <tr>bar</tr>
    <td>foo</td>
    <td class='theCell'>
      <div id='myDiv'>foo</div>
    </td>
  <tr>foo</tr>
  <tr>bar</tr>
</table>

これを行う良い方法を知っている人はいますか?前もって感謝します。

4

5 に答える 5

2

すべての div ID を配列に保存します。jsfiddle の使用.each

var dividlist = [];
$("td.theCell div").each(function() {
     dividlist.push(this.id);
});
于 2012-10-11T19:24:53.737 に答える
2

find を使用せずに子 div を直接選択することもできます。

$("td.theCell div").each(function() {    
      var id = this.id
});​
于 2012-10-11T19:25:12.400 に答える
2

これを試して

$("td.theCell").each(function() {
  var id = $(this).find('div').attr('id'); 
});

td.thecell 要素ではなく、divのIdを取得する必要があります。.find()そのため、その中のダイブを選択するために使用する必要があります..

于 2012-10-11T19:22:24.133 に答える
1

findを使用して子孫 div を取得します。

$("td.theCell").each(function() {
  var id = $(this).find('div').attr('id'); 
});
于 2012-10-11T19:22:23.143 に答える
0

セレクターを少し変更するだけでできます

$("#myTable tr > td.theCell > div").each(function() {//you can remove the '>' if you don't just want the direct children only 
  var k = $(this).attr('id'); 
  console.log(k);
});​
于 2012-10-11T19:28:13.827 に答える