获取元素的jquery索引-仅包括某些类型的元素

获取元素的jquery索引-仅包括某些类型的元素,jquery,indexing,Jquery,Indexing,我有一些这样的代码: <h2 id="a">Header</h2> <table> <tr> <td>test</td> </tr> </table> <h2 id="zzz">Header</h2> <table> <tr> <td>test</td> </tr> </tabl

我有一些这样的代码:

<h2 id="a">Header</h2>
<table>
  <tr>
    <td>test</td>
  </tr>
</table>

<h2 id="zzz">Header</h2>
<table>
  <tr>
    <td>test</td>
  </tr>
</table>

<h2 id="123">Header</h2>
<table>
  <tr>
    <td>test</td>
  </tr>
</table>

但它似乎不起作用。它仍然在计数中对DOM结构相同级别的其他元素进行计数。

您必须指定元素的集合,然后使用
index()
获取给定元素的索引。注意:参数必须是jQuery或DOM对象。字符串不起作用。
请注意,JavaScript索引是基于零的,因此
2
的索引实际上意味着第三个元素


Fiddle:

请记住,“123”不是ID属性的有效值。这是genius!!,你需要一个奖项,我想他们应该创造一个新闻方式来做到这一点,我的例子说明了这个答案有多好
$('#123').index('h2');
$("#123").parent().children('h2').index($('#123'))