JQuery:如何在单元格中选择表的行,而不是表的行

JQuery: How to select table rows but not the rows of the table inside the cell

本文关键字:选择 单元格 JQuery      更新时间:2024-05-24

所以我有一个表,单元格中也有表,如下所示:

<table id='table1'>
   <tr>
       <td>xyz</td>
       <td>
           <table><tr></tr></table>
       </td>
   </tr>
   <tr></tr>
   <tr></tr>
   <tr></tr>
</table>

如何使用jquery只选择"table1"下的那些,而不选择表内的那些?

您可以使用.children()

$('#table1').children('tr')

或子选择器

$('#table1 > tr')

这些将只选择直接子元素

正如@jonathanlonowski所说,由于浏览器添加了额外的tbody标记,因此使用它会更安全

$('#table1 > tr,#table1 > tbody > tr')

这也适用于

$('#table1').find('tr:first').siblings().andSelf()