2016-08-19 59 views
2

我有兩個問題。表格ID在DOM中不可用。如何在以下情況下使用Div id來獲得'10'的第3行第2列值?如何使用Div Id選擇器獲取表列值

我在以下代碼迭代通過第二列時出現錯誤。我如何獲得第三排第二列?

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