自定义 Web 组件 - 如何将其附加到的开头无论该组件在哪里使用?
一个用例是
<page-alert data-alertHeading='You are in danger' data-alertInfo ="This is a description of why you are in danger.">
</page-alert>
通常创建一个自定义页面警报组件,模板化的 HTML 将添加到页面中使用组件代码的位置:
this.attachShadow({mode: 'open' });
this.shadowRoot.appendChild(template.content.cloneNode(true));
但是,我想将此自定义组件附加到正文,以便它是第一个元素位于开始正文标记之后,并且始终显示在页面顶部。我怎样才能做到这一点?
A use case for this is creating a custom page alert component
<page-alert data-alertHeading='You are in danger' data-alertInfo ="This is a description of why you are in danger.">
</page-alert>
normally, the templated HTML would be added where the component code was used in the page with:
this.attachShadow({mode: 'open' });
this.shadowRoot.appendChild(template.content.cloneNode(true));
I however would like to append this custom component to the body so that it is the very first element following the opening body tag and will always appear at the top of the page. How can I go about accomplishing that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从组件内部:
从外部,如果您的元素已经在 DOM 中但需要移动:
From inside your component:
From the outside, if your element is already in the DOM but needs to be moved: