Jquery Select element 2 positions further - .next().next() 的另一种方式

问题描述 投票:0回答:2

我正在寻找一种方法来选择一个 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
选择器但没有找到解决方案。
稍后我也可能想在单击一个之后选择第 13 个(或第 23 个、第 65 个等等)。这意味着我想对这个问题有一个相当动态的解决方案。

感谢您的帮助,
菲尔

javascript jquery css-selectors jquery-selectors
2个回答
22
投票

您可以将

.nextAll()
.eq()
一起用于您的动态方法,如下所示:

$(this).nextAll().eq(1) //0 based index, this would be .next().next()

这将使您能够让

n
兄弟姐妹前进,这似乎就是您所追求的。


0
投票

看来

$(this).parent().find('div').eq(2).attr('id')
应该行得通。

更新(添加了 find('div') )

© www.soinside.com 2019 - 2024. All rights reserved.