gpt4 book ai didi

javascript - 在影子 DOM 元素中有影子 DOM 子元素

转载 作者:行者123 更新时间:2023-11-30 19:06:16 26 4
gpt4 key购买 nike

我正在尝试看看我是否可以构建一个主要由影子 DOM 元素组成的网站,一切正常,直到我尝试将一个影子 DOM 元素放入另一个影子 DOM 元素中

像这样

<body>
<nik-header background="#16a085" title="Custom HTML Components" color="#1c2127"></nik-header>
<nik-content background="#1c2127">
<button>nerd</button>
<nik-card title="nik"></nik-card>
</nik-content>
</body>

我的组件代码是这样的


//components.js

class nikHeader extends HTMLElement{
constructor() {
super();
var title = this.getAttribute('title');
var backgroundColor = this.getAttribute('background');
var textColor = this.getAttribute('color');
if(backgroundColor == null){
backgroundColor = "white"
}if(textColor == null){
textColor == "black"
}
this._root = this.attachShadow({mode: 'open'});
this._root.innerHTML = `
<div class="shadow-nik-header">
<center><h1>${title}</h1><center>
</div>

<style>

.shadow-nik-header{
position:absolute;
right:0;
left:0;
top:0;
height:80px;
background:${backgroundColor};
font-family:helvetica;
color:${textColor}
}
</style>

`;
}
}
class nikContent extends HTMLElement{
constructor(){
super();
var backgroundColor = this.getAttribute('background');
var textColor = this.getAttribute('color');
if(backgroundColor == null){
backgroundColor = "white"
}if(textColor == null){
textColor == "black"
}
this._root = this.attachShadow({mode: 'open'});
this._root.innerHTML = `
<div class="shadow-nik-content">

</div>

<style>

.shadow-nik-content{
position:absolute;
top:80px;
right:0px;
left:0px;
bottom:0px;
background:${backgroundColor};
color:${textColor};
}
</style>

`;
}
}
class nikCard extends HTMLElement{
constructor(){
super();
var backgroundColor = this.getAttribute('background');
var textColor = this.getAttribute('color');
var title = this.getAttribute('title');
var body = this.getAttribute('body');
var footer = this.getAttribute('footer')
if(backgroundColor == null){
backgroundColor = "white"
}if(textColor == null){
textColor == "black"
}
this._root = this.attachShadow({mode: 'open'});
this._root.innerHTML = `
<div class="shadow-nik-card">
<div class="shadow-nik-card-title">${title}</div>
<div class="shadow-nik-card-body">${body}</div>
<div class="shadow-nik-card-footer">${footer}</div>
</div>

<style>

.shadow-nik-card{
position:absolute;
background:blue;
}
</style>

`;
}
}


window.customElements.define('nik-card', nikCard);
window.customElements.define('nik-content', nikContent);
window.customElements.define('nik-header', nikHeader);

我在 <nik-content></nik-content> 中放置的按钮标签不显示在元素的边界内它只是在顶部没有任何影响我还注意到实际元素没有任何大小或位置除非我检查并向下滚动到谷歌浏览器的阴影元素部分是否可以在影子 DOM 父级中包含影子 DOM 子级?还是只能将它们放在常规元素中?

最佳答案

您忘记使用 <slot>容器的 Shadow DOM HTML 定义中的元素 <nik-content> .结果,里面没有插入任何东西。 Shadow DOM 隐藏了 light DOM。

this._root.innerHTML = `
<div class="shadow-nik-content">
<slot></slot>
</div>
...
`;

关于javascript - 在影子 DOM 元素中有影子 DOM 子元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58959091/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com