简体   繁体   English

附加影子 DOM 时如何保留原始 DOM 子级?

[英]How to keep original DOM children when attaching a shadow DOM?

I have a root div element with two children:我有一个带有两个孩子的根div元素:

<div id = "root">
  <div id="firstChild">FirstChild</div>
  <div id="secondChild">SecondChild</div>
</div>

I attach a shadow DOM to root and append another child to the shadow DOM.我将shadow DOM附加到root并将另一个子级附加到影子 DOM。 I would expect to see all three children:我希望看到所有三个孩子:

ShadowChild
FirstChild
SecondChild

or要么

FirstChild
SecondChild
ShadowChild

However, attaching the shadow seems to hide the original children of the root div :但是,附加阴影似乎隐藏了根div的原始子级:

 ShadowChild

=>How can I keep/show the original children? =>我怎样才能保留/展示原来的孩子?

The following code example might require Chrome browser to support the method attachShadow :以下代码示例可能需要 Chrome 浏览器支持方法attachShadow

 var root = document.getElementById('root') var shadowRoot = root.attachShadow({mode: 'open'}); var shadowChild = document.createElement('div'); shadowChild.innerText ='ShadowChild'; shadowRoot.appendChild(shadowChild);
 <div id = "root"> <div id="firstChild">FirstChild</div> <div id="secondChild">SecondChild</div> </div>

Edit编辑

Here it says这里

While the children of a shadow host do not generate boxes normally, they can be explicitly pulled into a shadow tree and forced to render normally.虽然 shadow host 的 children 不会正常生成框,但可以将它们显式拉入 shadow tree 并强制正常渲染。 This is done by assigning the elements to a distribution list.这是通过将元素分配给分发列表来完成的。 An element with a distribution list is an insertion point.具有分发列表的元素是插入点。

=>How can I do so for a custom html element? => 如何为自定义 html 元素执行此操作?

My use case is a custom tab folder that I want to use like我的用例是我想使用的自定义选项卡文件夹

<treez-tab-folder id="root">
   <treez-tab title="First tab">
        <div id='firstContent'>First tab content</div>
    </treez-tab>

    <treez-tab title="Second tab">
        <div>Second tab content</div>
    </treez-tab>
</treez-tab-folder>

在此处输入图像描述

I create headers for the tabs in a shadow dom of treez-tab-folder , using the title attribute.我使用 title 属性为treez-tab-folder的影子 dom 中的选项卡创建标题。 The creation of the headers works, but the tabs can not be seen any more.标题的创建有效,但无法再看到选项卡。 I could copy the original treez-tab children to the shadow DOM.我可以将原始的treez-tab子项复制到影子 DOM。 But what if another tab is dynamically added to the tab folder later?但是,如果稍后将另一个选项卡动态添加到选项卡文件夹中怎么办?

d3.select("#root").append('treez-tab'); 

As already stated I would like to enable the rendering of the original children or automatically "redirect/proxy" the original children to the shadow DOM.如前所述,我想启用原始子项的渲染或自动将原始子项“重定向/代理”到影子 DOM。

Related question, including the tab folder example without a shadow dom:相关问题,包括没有影子 dom 的选项卡文件夹示例:

How to target custom element (native web component) in vue.js? 如何在 vue.js 中定位自定义元素(本机 Web 组件)?

Not using a shadow DOM would have the drawback that the original treez-tab-folder includes additional DOM elements, possibly causing issues (eg with vue.js).不使用影子 DOM 会有一个缺点,即原始的treez-tab-folder包含额外的 DOM 元素,可能会导致问题(例如 vue.js)。

For anyone wondering today: was answered here .对于今天想知道的人:在这里得到回答。
Basically, a <slot> element to the shadow DOM.基本上,影子 DOM 的<slot>元素。

 var root = document.getElementById('root') var shadowRoot = root.attachShadow({mode: 'open'}); var wrapper = document.createElement('slot'); shadowRoot.appendChild(wrapper); var shadowChild = document.createElement('div'); shadowChild.innerText ='ShadowChild'; shadowRoot.appendChild(shadowChild);
 <div id="root"> <div id="firstChild">FirstChild</div> <div id="secondChild">SecondChild</div> </div>

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

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