从div容器内的所有表中循环td? JQuery的

时间:2017-09-28 19:25:14

标签: javascript jquery for-loop

我正在尝试遍历3个不同的表并获得td个ID。所有三个表都在同一个div容器中。我在所有三个表上都有相同的类。这是一个例子:

<div id="myContainer">
  <table class="myTbl">
     <thead>
        <tr>
          <th>Text 1</th>
        </tr>
     </thead>
     <tbody>
       <tr>
          <td id="el_1"></td>
       </tr>
     </tbody>
  </table>
  <table class="myTbl">
     <thead>
        <tr>
          <th>Text 2</th>
        </tr>
     </thead>
     <tbody>
       <tr>
          <td id="el_2"></td>
       </tr>
     </tbody>
  </table>
  <table class="myTbl">
     <thead>
        <tr>
          <th>Text 3</th>
        </tr>
     </thead>
     <tbody>
       <tr>
          <td id="el_3"></td>
       </tr>
     </tbody>
  </table>
</div>

这是我的JQuery。下面的代码只循环遍历第一个表

$('table.myTbl tbody tr td').each(function(){
    console.log($(this).prop('id'))
});
然后我尝试了这个。这与上面相同。只有第一个表中的id显示在控制台中

$('#myContainer table tbody tr td').each(function(){
    console.log($(this).prop('id'))
});

我想知道如何在td容器内的所有表中循环div id?

0 个答案:

没有答案