Skip recursion in jQuery.find() for a selector?

后端 未结 8 1067
孤独总比滥情好
孤独总比滥情好 2020-12-18 05:31

TL;DR: How do I get an action like find(), but block traversal (not full stop, just skip) for a certain selector?

ANSWERS: $(Any).fin

8条回答
  •  再見小時候
    2020-12-18 06:12

    Maybe something like this would work:

    $.fn.findExclude = function (Selector, Mask) {
        var result = new jQuery();
        $(this).each(function () {
            var $selected = $(this);
            $selected.find(Selector).filter(function (index) {
                var $closest = $(this).closest(Mask);
                return $closest.length == 0 || $closest[0] == $selected[0] || $.contains($closest, $selected);
            }).each(function () {
                result.push(this);
            });
        });
        return result;
    }
    

    http://jsfiddle.net/JCA23/

    Chooses those elements that are either not in mask parent or their closest mask parent is same as root or their closest mask parent is a parent of root.

提交回复
热议问题