如何获取表中具有相同类选择器的所有行?

时间:2019-07-02 15:31:07

标签: javascript jquery html

我正在尝试从与具有相同类选择器的许多其他表动态生成的表中获取行。

我尝试使用类选择器$('。test'),但是它使用选择器$('。test')渲染所有表中的所有tr

2 个答案:

答案 0 :(得分:1)

为表格添加唯一的内容(唯一类,id…),并在选择器中使用它。

$("#the-table .test");

如果由于生成而不能添加它,但是如果表是同级的,则可以使用:nth-​​child()选择器。

$("table:nth-child(3) .test");

答案 1 :(得分:0)

首先将数据表放入表中,将数据行放入行中,诸如:

$('[data-trigger]').click(function(){
$('[data-table]').find('[data-row]').toggleClass('change');
});
tr.change td{
background-color:coral;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table data-table>
  <tbody>
    <tr data-row>
      <td>Hi</td>
    </tr>
     <tr data-row>
      <td>Bye</td>
    </tr>
  </tbody>
</table>
<button type="button" data-trigger>Change color of rows</button>