我正在寻找一种方法来选择一个 div 元素,它不是通过单击功能“选择”的元素的直接下一个元素。
<div id="click">(siblings)</div><div>text</div><div id="get this one"></div>
现在我想选择 ID 为“get this one”的那个——在我的代码中这个 ID 不可用。所有的 div 都有相同的类,并且有兄弟姐妹。
我可以通过
$(this).next().next()
选择第三个,但我认为这不是最好的方法。div
s - 所以它不一定是第一个。
我尝试了
:nth-child
选择器但没有找到解决方案。感谢您的帮助,
菲尔
.nextAll()
与 .eq()
一起用于您的动态方法,如下所示:
$(this).nextAll().eq(1) //0 based index, this would be .next().next()
这将使您能够让
n
兄弟姐妹前进,这似乎就是您所追求的。
看来
$(this).parent().find('div').eq(2).attr('id')
应该行得通。
更新(添加了 find('div') )