英文:
Manual slotAssignment doesn't assign contents
问题
<!-- 开始代码片段: js 隐藏: false 控制台: true Babel: false -->
<!-- 语言: lang-js -->
class RealTodo extends HTMLElement {
constructor() {
super();
this.attachShadow({ mode: "open", slotAssignment: "manual" });
}
connectedCallback() {
const todoTemplate = document.getElementById("todo_template");
const todoApp = todoTemplate.content.cloneNode(true);
this.shadowRoot.appendChild(todoApp);
const slot = this.shadowRoot.querySelector("slot");
console.log(slot);
const li = document.createElement("li");
li.textContent = "Buy food";
slot.assign(li);
}
}
customElements.define("real-todo", RealTodo);
<!-- 语言: lang-html -->
<template id="todo_template">
<h1>TODO LIST</h1>
<ol>
<slot></slot>
</ol>
</template>
<real-todo></real-todo>
<!-- 结束代码片段 -->
我尝试手动分配阴影DOM内的内容。
它没有分配。没有显示错误。
我在Firefox和Chrome中进行了测试。
根据规范 slot.assign(nodes);
将分配到 slotAssignment: "manual"
阴影DOM 内。
英文:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
class RealTodo extends HTMLElement {
constructor() {
super();
this.attachShadow({ mode: "open", slotAssignment: "manual" });
}
connectedCallback() {
const todoTemplate = document.getElementById("todo_template");
const todoApp = todoTemplate.content.cloneNode(true);
this.shadowRoot.appendChild(todoApp);
const slot = this.shadowRoot.querySelector("slot");
console.log(slot);
const li = document.createElement("li");
li.textContent = "Buy food";
slot.assign(li);
}
}
customElements.define("real-todo", RealTodo);
<!-- language: lang-html -->
<template id="todo_template">
<h1>TODO LIST</h1>
<ol>
<slot></slot>
</ol>
</template>
<real-todo></real-todo>
<!-- end snippet -->
I try to assign content inside shadowDom's slot manually.
It doesn't assign. No error show.
I tested in firefox, chrome.
According to spec slot.assign(nodes);
will assign inside slotAssignment: "manual"
shadowdom
答案1
得分: 1
Your "const li = document.createElement("li");" creates a Node in memory
"SLOTElement.assign()" only accepts existing DOM Element(s) in
first level lightDOM
And you don't want to use "appendChild", because that moves a DOM Element
英文:
Your const li = document.createElement("li");
creates a Node in memory
SLOTElement.assign()
only accepts existing DOM Element(s) in
first level lightDOM
And you don't want to use append[Child]()
, because that moves a DOM Element
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
customElements.define("real-todo", class extends HTMLElement {
constructor() {
const button = (props) => Object.assign(document.createElement("button"),props);
super()
.attachShadow({mode: "open", slotAssignment: "manual" })
.append(
button({ innerHTML:"Food" , onclick : e => this.showlist("food") }),
button({ innerHTML:"Animals", onclick : e => this.showlist("animal") }),
document.getElementById(this.nodeName).content.cloneNode(true)
);
}
connectedCallback(){
this.showlist();
}
showlist( list=this.getAttribute("list") ){
this.shadowRoot.querySelector("span").innerHTML = list;
const li = this.querySelectorAll(`li[list*="${list}"]`);
this.shadowRoot.querySelector("slot").assign(...li);
}
});
<!-- language: lang-html -->
<template id="REAL-TODO">
<h1>A <span></span> list</h1>
<ol>
<slot></slot>
</ol>
</template>
<real-todo list="animal">
<li list="animal">Cat</li>
<li list="animal">Dog</li>
<li list="food">Pie</li>
<li list="food">Beer</li>
<li list="food">Pizza</li>
<li list="animal,food">Chicken</li>
<div>
<li list="animal,food">Never listed!!</li>
</div>
</real-todo>
<!-- end snippet -->
The inspector will show which LI
elements are slotted:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论