how to get all parent nodes of given element in pure javascript?

前端 未结 6 2174
执笔经年
执笔经年 2020-12-08 10:01

I mean an array of them. That is a chain from top HTML to destination element including the element itself.

for example for element it would b

6条回答
  •  失恋的感觉
    2020-12-08 10:59

    I like this method:

    [...(function*(e){do { yield e; } while (e = e.parentNode);})($0)]
    

    ... where $0 is your element.

    An upside of this method is that it can be used as a value in expressions.

提交回复
热议问题