Similar to jQuery .closest() but traversing descendants?

后端 未结 16 748
情书的邮戳
情书的邮戳 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:58

    If by "closest" descendant you mean the first child then you can do:

    $('#foo').find(':first');
    

    Or:

    $('#foo').children().first();
    

    Or, to look for the first occurrence of a specific element, you could do:

    $('#foo').find('.whatever').first();
    

    Or:

    $('#foo').find('.whatever:first');
    

    Really though, we need a solid definition of what "closest descendant" means.

    E.g.

    Which would $('#foo').closestDescendent('span') return?

提交回复
热议问题