How to recursively search all parentNodes

前端 未结 5 599
耶瑟儿~
耶瑟儿~ 2020-12-14 02:16

I want to know, how to find out recursively all parent nodes of an element. Suppose i have following snippet

Hello

5条回答
  •  暖寄归人
    2020-12-14 02:56

    You can use jQuery closest() method to get the closest ahref:

    $("#your-element").closest("a").css("color", "red");
    

    Or you can have a look at the parentsUntil method:

    $("#your-element").parentsUntil("#yourWrapper", "a").first().css("color", "red");
    

    Try it out here: http://www.lunarailways.com/demos/parents.html

提交回复
热议问题