javascript - How do I traverse up out of shadow dom? - Stack Overflow

I have a structure:<div>#shadow-root<span>How do I reference the div from the span?span.par

I have a structure:

<div>
    #shadow-root
        <span>

How do I reference the div from the span?

span.parentNode is the #shadow-root, and .parentNode from that is null

I have a structure:

<div>
    #shadow-root
        <span>

How do I reference the div from the span?

span.parentNode is the #shadow-root, and .parentNode from that is null

Share Improve this question asked Jul 15, 2014 at 18:10 user578895user578895 7
  • 1 That makes no sense: jsfiddle/9FzAM – Ian Commented Jul 15, 2014 at 18:17
  • 1 @Ian your example is the regular DOM; this question is about shadow-DOM (enable in chrome devtools.) – Evan Davis Commented Jul 15, 2014 at 18:22
  • @Mathletics A ha! I seriously had no idea what they meant by "shadow dom" (the # made me think "id"), and somehow missed the "shadow-dom" and "polymer" tags. Thanks for pointing that out – Ian Commented Jul 15, 2014 at 18:23
  • 3 shadowRoot.host would give you the <div>. Likewise, div.shadowRoot would get you the div's shadow root. – ebidel Commented Jul 15, 2014 at 18:25
  • 1 @ebidel Good call. You can see host and parentNode properties of the shadow root with console.dir(child.parentNode); – Ian Commented Jul 15, 2014 at 18:27
 |  Show 2 more ments

2 Answers 2

Reset to default 6

You can use shadowRoot.host to give you the <div>. Likewise, div.shadowRoot would get you the div's shadow root.

First find shadowRoot host from the element el.getRootNode().host and then use closest()

el.getRootNode().host.closest('div.my_div')

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745229004a4617593.html

相关推荐

  • javascript - How do I traverse up out of shadow dom? - Stack Overflow

    I have a structure:<div>#shadow-root<span>How do I reference the div from the span?span.par

    4小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信