如何使用Shadow DOM v1从影子根内部访问主机元素? [英] How to access host element from within the shadow root using Shadow DOM v1?

查看:96
本文介绍了如何使用Shadow DOM v1从影子根内部访问主机元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出影子根目录中包含的元素,我如何到达托管所述影子根目录的元素?

Given an element contained in a shadow root, how can I get to the element that hosts the said shadow root? Is there a single way to accomplish this regardless of where an element is in the tree (i.e. given a reference to either element2 or element3, get the reference to element1)?

element1
└ #shadow-root
  └ element2
    └ element3

推荐答案

对于Shadow DOM v1,您可以使用 getRootNode() 方法.

For Shadow DOM v1, you may use the getRootNode() method.

然后获取host属性:

event.target.getRootNode().host

这篇关于如何使用Shadow DOM v1从影子根内部访问主机元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆