2016-08-19 8 views
2

質問が2つあります。テーブルIDはDOMでは使用できません。次のような場合にDiv IDを使用して3行目の2列目の値「10」を取得するにはどうすればよいですか?Div Idセレクタを使用してテーブルの列値を取得する方法

2番目の列を反復している次のコードでエラーが発生します。 3行目、2列目のみを取得するにはどうすればよいですか?

のjQueryコード

<script> 
    $("'#part_li3_tt table'").children("tbody").children("tr").children("td:nth-child(2)").each(function() { 
     alert("third row second col Value: " + $(this).html()); 
    }); 
</script> 

HTMLコード

<div id="part_li3_tt"> 
    <table> 
     <tbody> 
      <tr> 
       <td>FName:</td> 
       <td>Sara</td> 
      </tr> 
      <tr> 
       <td>LName:</td> 
       <td>Rodriguez</td> 
      </tr> 
      <tr> 
       <td>Class:</td> 
       <td>10</td> 
      </tr> 

     </tbody> 
    </table> 
</div> 

答えて

2

あなたはこのように、jQueryのnth-of-typeセレクタを使用することができるはずです。

alert($('#part_li3_tt table tr:nth-of-type(3) td:nth-of-type(2)').html()); 

ホーwever、あなたが最初の場所でID part_li3_ttを必要とする理由私はわからないので、あなたの代わりにこれを行うことができるかもしれない:

alert($('table tr:nth-of-type(3) td:nth-of-type(2)').html()); 

あなたはnth-of-type jQueryのセレクタがどのように機能するかについて混乱している場合は、することができます詳細はこちらをご覧ください: https://api.jquery.com/nth-of-type-selector/

+0

ありがとうございます! @Xetnus – Kaur

関連する問題