Get siblings between 2 elements

后端 未结 4 419
旧时难觅i
旧时难觅i 2021-01-11 17:01

How can I get the elements between 2 other elements?

Like if I had:

4条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-11 17:47

    You might want to first check if elements come in the proper order, othenwise nextUntil will return unexpected set of elements.

    /**
     * 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;
        };
    

提交回复
热议问题