nearest ancestor node in jQuery

后端 未结 4 1739
情歌与酒
情歌与酒 2021-01-07 23:53

In my javascript experience, I found that is a very common task \"searching the nearest ancestor of an element with some condition (tag name, class,...)\". Can the parents(

4条回答
  •  无人及你
    2021-01-08 00:43

    Adding to @nickf's answer:

    jQuery 1.3 simplifyed this task with closest.

    Given a DOM:

    You can do:

    $('#d').closest("div"); // returns [ div#b ]
    

    [Closest returns a] set of elements containing the closest parent element that matches the specified selector, the starting element included.

提交回复
热议问题