Web组件:操纵阴影DOM的CSS

发布于 2025-02-03 22:28:10 字数 588 浏览 3 评论 0原文

我正在使用iframe标签的Web组件。

例如:

<my-component>
  <!-- SHADOW DOM -->
  <iframe>
    ...
  </iframe>
</my-component>

现在,当我尝试样式my-component如下所示时,它似乎不适用。

my-component {
  cursor: default;
}

默认行为是光标:指针,我想摆脱它。 访问Shadow dom,例如吗?

my-component > iframe {
  cursor: default;
}

我可以直接通过CSS 不确定是否可能发生这样的事情。

I am using a web component that has an iframe tag in its root.

For example:

<my-component>
  <!-- SHADOW DOM -->
  <iframe>
    ...
  </iframe>
</my-component>

Now when I try to style my-component as below, it does not seem to apply.

my-component {
  cursor: default;
}

The default behaviour is cursor: pointer and I want to get rid of it. Could I access the shadow DOM via css directly, such as follows?:

my-component > iframe {
  cursor: default;
}

Even if the cursor: pointer is in some deep nested HTML element, I could try and find the target this way, but I'm not sure if such a thing is even possible.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

甜心 2025-02-10 22:28:10

要修改嵌套的Web组件,包含组件的作者必须通过:: part()来公开这些组件(在此处阅读更多有关它的信息在阴影中的样式 - dom-with-with-css-shadow-parts/#aa-styling-shadow-dom-with-part“ rel =” nofollow noreferrer“> https://css-tricks.com/stylicks.com/styling--在阴影中 - dom-with-with-css-shadow-parts/#aa-styling-shadow-dom-with-with-part )。

否则,您必须使用JavaScript来样式的嵌套Web组件。

const myComponent = document.querySelector('my-component');
const childIFrame = myComponent.shadowRoot.querySelector('iframe');

childIFrame.style.cursor = default;

如果要到达嵌套在另一个Web组件中的Web组件的阴影,则必须使用ShadowRoot进一步挖掘:

const myComponent = document.querySelector('my-component');
const childComponent = myComponent.shadowRoot.querySelector('child-component');
const childIFrame = childComponent.shadowRoot.querySelector('iframe');

childIFrame.style.cursor = default;

To modify nested web components the author of the containing component must expose these, e.g. through ::part() (read more about it here https://css-tricks.com/styling-in-the-shadow-dom-with-css-shadow-parts/#aa-styling-shadow-dom-with-part).

Otherwise, you have to use JavaScript to style nested web components.

const myComponent = document.querySelector('my-component');
const childIFrame = myComponent.shadowRoot.querySelector('iframe');

childIFrame.style.cursor = default;

If you want to reach the shadowdom of a web component nested inside another web component, you have to dig further using shadowRoot:

const myComponent = document.querySelector('my-component');
const childComponent = myComponent.shadowRoot.querySelector('child-component');
const childIFrame = childComponent.shadowRoot.querySelector('iframe');

childIFrame.style.cursor = default;

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文