3

我有一个表结构:

<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 和 link 标签的每个表格行列,但我没有达到目标。我一直在做的最好的是:

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

关于我应该阅读什么或应该如何处理这个问题的任何建议?

谢谢帕里贾特·卡利亚

4

5 回答 5

2
$('#cust-id td[id] a').each(function () {
  var td = $(this).closest('td');
  // do what you want
});
于 2012-08-24T15:52:14.740 回答
1

试试这个

$("#cust-id tr:has(td[id] a)");
于 2012-08-24T15:56:34.530 回答
0
$('#cust-id td')

将收集td元素下的所有元素#cust-id。您可以像在常规 CSS 中一样链接选择器。

如果您想要 a 的父母,您可以从using<td>遍历回来<td>

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

啊,你实际上只想要那些<td><a>和 的id,所以......

$('#cust-id td[id] a').closest('td')
于 2012-08-24T15:50:09.543 回答
0

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

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

这将选择所有具有“id”属性的 TD 元素:

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

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

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

因此,如果结合这些方法,您可以选择那些 (1) 具有“id”属性并且 (2) 包含元素的 TD<a>元素:

$( 'td[id]:has(a)', '#cust-id' )
于 2012-08-24T15:52:01.113 回答
0

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

于 2012-08-24T15:52:04.927 回答