简体   繁体   English

是否可以访问 SVG 的影子 dom

[英]Is it possible to access the shadow dom of an SVG

Here is an svg with a use element:这是一个带有使用元素的 svg:

 <svg viewBox="0 0 30 10" xmlns="http://www.w3.org/2000/svg"> <circle id="myCircle" cx="5" cy="5" r="4" stroke="blue"/> <use href="#myCircle" x="10" fill="blue"/> </svg>

In the inspector I can see that the use element has a shadow root:在检查器中,我可以看到 use 元素有一个影子根:

<use href="#myCircle" x="10" fill="blue">
  #shadow-root (closed)
  <circle id="myCircle" cx="5" cy="5" r="4" stroke="blue"></circle>
</use>

Is it possible to somehow call methods such as getBBox() on the <circle> in the shadow dom?是否可以在影子 dom 中的<circle>上以某种方式调用诸如getBBox()之类的方法?

If there is no straight-forward way, maybe there is some trick to allow me to access the closed shadow root?如果没有直接的方法,也许有一些技巧可以让我访问封闭的影子根?

In this case, it looks like no :在这种情况下,它看起来像no

If you attach a shadow root to a custom element with mode: closed set, you won't be able to access the shadow DOM from the outside — myCustomElem.shadowRoot returns null .如果您将影子根附加到具有mode: closed集的自定义元素,您将无法从外部访问影子 DOM — myCustomElem.shadowRoot返回null This is the case with built in elements that contain shadow DOMs, such as <video> [or svg].包含影子 DOM 的内置元素就是这种情况,例如<video> [或 svg]。

Source: https://developer.mozilla.org/en-US/docs/Web/Web_Components/Using_shadow_DOM#Basic_usage来源: https://developer.mozilla.org/en-US/docs/Web/Web_Components/Using_shadow_DOM#Basic_usage

Since the web inspector is listing the shadow root as closed, it isn't accessible via javascript.由于 web 检查器将影子根列为已关闭,因此无法通过 javascript 访问它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM