Jquery选择元素直到类“B”或类“A”,具体取决于首先找到的元素

时间:2014-03-11 11:38:30

标签: javascript jquery dom

我知道我可以使用nextUntil()从特定元素开始选择所有元素,直到找到特定元素。例如

// select all the elements between class A and class B
$('.A').nextUntil('.B');

现在考虑我要从具有类A的元素中选择所有元素,直到找到具有类B的元素,但是如果存在另一个元素,我希望停止此搜索找到班级A。例如,假设我有以下元素结构(类)

A
C
D
E
A  <-- Stop the search and return all the elements till here.
B

正如您所看到的,如果在A之前找到搜索,我希望在下一个B停止搜索,但是如果找不到此A,则查找下一个元素,直到B

A
C
D
E
B  <-- Stop the search and return all the elements till here.
C
A
D
E

我一直在努力实现这一目标,但我无法做到这一点。有没有办法可以使用jquery nextUntil()来实现这个优先级明智的选择?

1 个答案:

答案 0 :(得分:3)

尝试使用简单的多选择器语法

$('.A').nextUntil('.B, .A');