2016-08-19 24 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