Similar to jQuery .closest() but traversing descendants?

后端 未结 16 727
情书的邮戳
情书的邮戳 2020-12-07 15:13

Is there a function similar to jQuery .closest() but for traversing descendants and returning only closest ones?

I know that there is

16条回答
  •  粉色の甜心
    2020-12-07 15:46

    Rob W's answer didn't quite work for me. I adapted it to this which did work.

    //closest_descendent plugin
    $.fn.closest_descendent = function(filter) {
        var found = [];
    
        //go through every matched element that is a child of the target element
        $(this).find(filter).each(function(){
            //when a match is found, add it to the list
            found.push($(this));
        });
    
        return found[0]; // Return first match in the list
    }
    

提交回复
热议问题