遍历jQuery中的表元素

Traversing table elements in jQuery

本文关键字:元素 jQuery 遍历      更新时间:2023-09-26

我有一个表结构为:

<table id = "cust-id">
  <tr>
    <td> 1</td>
    <td id = "specific_id_re"><a href = "#">link tag</a></td>
  </tr>
  <tr>
    <td> 2 </td>
    <td id = "specific_id"> <a href = "#">link tag</a></td>
  </tr>
</table>

我正试图使用jquery来访问每个具有id和链接标记的表行列,但我做不到。我做得最好的是:

 $('#cust-id').children().children().children() ; // to get access to the td elements ?

关于我应该读什么或如何处理这件事,有什么建议吗?

谢谢Parijat Kalia

$('#cust-id td[id] a').each(function () {
  var td = $(this).closest('td');
  // do what you want
});

试试这个

$("#cust-id tr:has(td[id] a)");

您可以使用以下选择器$('#cust id td[id]'),这将返回所有具有id属性的td元素。

$('#cust-id td')

将收集具有CCD_ 2的元素下的所有CCD_。您可以像在常规CSS中一样链接选择器。

如果您想要<td>父级,您可以使用从<td>向上遍历

$('#cust-id td').closest('tr')

啊,你实际上只想要那些有<a>id<td>,所以…

$('#cust-id td[id] a').closest('td')

这将选择2中的所有TD元素。列:

$( 'td:nth-child(2)', '#cust-id' )

这会选择所有具有"id"属性的TD元素:

$( 'td[id]', '#cust-id' )

这将选择包含<a>元素的所有TD元素:

$( 'td:has(a)', '#cust-id' )

因此,如果您将这些方法结合起来,您可以选择(1)具有"id"属性,(2)包含<a>元素的TD元素:

$( 'td[id]:has(a)', '#cust-id' )