遍历jQuery中的表元素

时间:2012-08-24 15:48:14

标签: javascript jquery html dom dom-traversal

我有一个表结构:

<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

5 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:1)

试试这个

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

答案 2 :(得分:0)

$('#cust-id td')

将使用td收集元素下的所有#cust-id元素。您可以将选择器与常规CSS链接起来。

如果您想要<td>父母,您可以使用

<td>进行追溯
$('#cust-id td').closest('tr')

啊,你实际上只想要那些<td><a>的{​​{1}},所以......

id

答案 3 :(得分:0)

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

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

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

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

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

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

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

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

答案 4 :(得分:0)

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

相关问题