Select 使用 jQuery 在兄弟 div 之间的第一个匹配树

Select the first matching down the tree among sibling divs using jQuery

假设我有

<div class="x">
    <div class="y"></div>
    <div class="y"></div>
    <div class="y"></div>
    <div class="y"></div>
    <div class="y"></div>
    <div class="y"></div>
    <div class="z"></div>
    <div class="y"></div>
    <div class="y"></div>
    <div class="y"></div>
    <div class="z"></div>
</div>

点击 .y 我需要 select .z ,这是顺序中的第一个。

之后应该写什么
$(this). ?

其中 this 是我点击的 div

您可以使用 nextAll() 方法,如下所示。

$(this).nextAll('.z:first')

使用jQuery的nextAllfirst函数:

var z = $(this).nextAll('.z').first();

了解更多:

https://api.jquery.com/nextAll/

https://api.jquery.com/first/

.next() 将从树向下 select,.closest() 将搜索最近的祖先,尝试:

$(this).next("div.z");