获得2个元素之间的兄弟姐妹

如何获得2个其他元素之间的元素?

就像我有:

我想获得div.c3div.c6之间的元素。

使用nextUntil [docs]

 var $siblings = $('.c3').nextUntil('.c6'); 

您可能希望首先检查元素是否按正确顺序排列,然后nextUntil将返回意外的元素集。

 /** * Returns an array of sibling elements between `from` and `to` including `from` and `to` */ function getElementsInBetween (from, to) { var range = []; if (from.index() > to.index()) { //ensure that 'from' is before 'to' from = [to, to = from][0]; //swapping from and to } if (from.is(to)) { range = [from]; } else { range = from.nextUntil(to).add(from).add(to); range = $.makeArray(range); } return range; }; 

如果它仅用于样式目的,并且您不想使用脚本,则可以这样做。

假设你想要在.c3.c6之间.c3元素的.c6

 .c3 ~ div { /*special styling*/ } div, .c6 ~ div { /*normal styling*/ } 

你可以在这里看到它

您还可以尝试以下方法:

 var selected_elems = $("div").slice(2,6); 

slice() :将匹配元素集减少到由一系列索引指定的子集。

文档: http : //api.jquery.com/slice/