jQuery获取下一个元素及其所有子元素

6
我该如何获取下一个div元素及其所有子元素?
<div class="1"></div>
<div class="2">
    <div class="child1"></div>
    <div class="child2"></div>
</div>
<div class="3">
    <div class="child1"></div>
</div>

如果当前 $(this) 有 class 1,我要怎么样才能只获取 class 2 及其所有子元素。因为我不知道它的 class 名称,所以无法通过类名引用它。

我基本上想把这两个语句合成一个。

$(this).next('div').animate({height : 'toggle'});
$(this).next('div *').animate({height : 'toggle'});

2
http://api.jquery.com/category/traversing/ - Blazemonger
1个回答

13
$(this).next('div').children().andSelf().animate({height : 'toggle'});

谢谢Matt,这很好用。我需要理解一下this是如何变成$(this).next()的,以便在andSelf()中作为原始this的兄弟节点引用自身。 - Tim Joyce
简短回答:jQuery 维护一个内部选定对象的堆栈。像 .next().children() 这样的方法会推入堆栈;.end() 弹出。我推断 .andSelf() 在堆栈中向下查找一级。 - Matt Ball
不用谢。阅读.end()文档可以提供更多细节。 - Matt Ball

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接