阴影dom不依附在htmlbuttonelement继承的类上
我试图将阴影DOM附加到此处,但是它不附加,并且不支持domexpection操作,但是当从htmlelement或htmlspanelement继承时,它起作用了! 因此,问题是,当从htmlbuttonelement继承时,我可以将阴影DOM附加到此,如果是的,那么为什么不这样做! 这是我的代码: -
class Home_Button extends HTMLButtonElement {
constructor() {
super();
this._dom = this.attachShadow({ mode: "open" });
this.createButton();
}
createButton() {
this.classList.add("contents");
const style = document.createElement("style");
style.textContent = `
.sentToHome{
margin: 1%;
padding: 1%;
border-radius: 5px;
border: 1px solid black;
}
.homeLink{
text-decoration: none;
color: green;
}
`;
const anchor = document.createElement("a");
anchor.innerText = "<< Home";
anchor.setAttribute("href", "/");
anchor.setAttribute("class", "homeLink");
const button = document.createElement("button");
button.setAttribute("class", "sentToHome");
button.appendChild(anchor);
this._dom.append(style, button);
}
}
customElements.define("simple-btn", Home_Button, { extends: "button" });
const sentToHomeBtn = new Home_Button();
document.body.appendChild(sentToHomeBtn);
I am trying to attach shadow dom to this but it does not attach and gives a error DOMExpection operation is not supported but when inherit from HTMLElement or HTMLSpanElement it works!
So question is can i attach shadow dom to this when inherit from HTMLButtonElement if yes then how else not why!
here is my code :-
class Home_Button extends HTMLButtonElement {
constructor() {
super();
this._dom = this.attachShadow({ mode: "open" });
this.createButton();
}
createButton() {
this.classList.add("contents");
const style = document.createElement("style");
style.textContent = `
.sentToHome{
margin: 1%;
padding: 1%;
border-radius: 5px;
border: 1px solid black;
}
.homeLink{
text-decoration: none;
color: green;
}
`;
const anchor = document.createElement("a");
anchor.innerText = "<< Home";
anchor.setAttribute("href", "/");
anchor.setAttribute("class", "homeLink");
const button = document.createElement("button");
button.setAttribute("class", "sentToHome");
button.appendChild(anchor);
this._dom.append(style, button);
}
}
customElements.define("simple-btn", Home_Button, { extends: "button" });
const sentToHomeBtn = new Home_Button();
document.body.appendChild(sentToHomeBtn);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这将永远不会在Safari中起作用,因为Apple自2016年以来表示,他们永远不会实现自定义的内置元素,他们仅实现了
htmlelement
触发create button 代码> ConnectedCallback ;您不能在
构造函数
(除Shadowdom内容)中执行DOM操作,因为DOM中还不存在元素(this
)。另外,请注意
this._dom
不需要,您可以免费从thisever> entachshadow()
免费获得this.shadowroot
要清楚
this。 classList
是问题This will never work in Safari, because Apple has, since 2016, stated they will never implement Customized Built-In Elements, they have only implemented
extends HTMLElement
Trigger
createButton
from theconnectedCallback
; you can't do DOM operations in theconstructor
(besides shadowDOM content) because the element (this
) doesn't exist in the DOM yet.Also note
this._dom
isn't required, you getthis.shadowRoot
for free fromattachShadow()
To be clear
this.classList
is the problem好吧, @danny-365csi-engelman 也是正确的
我发现此答案。
根据答案,我们可以在几个元素上连接Shadow dom,并且按钮不退出这些元素。
我只是从 htmlelement 类而不是 htmlbuttonelement 类继承。
之后,一切都很好!
如果您想检查当前源代码go 在这里。
Well, @danny-365csi-engelman is also right but
I found this answer.
According to the answer we can attach shadow dom on few elements and button does not exits in those elements.
I just inherit from HTMLElement class instead of HTMLButtonElement class.
After that everything works fine!.
If you want to check current source code go here.