阴影dom不依附在htmlbuttonelement继承的类上

发布于 2025-02-08 03:38:38 字数 1235 浏览 4 评论 0原文

我试图将阴影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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

茶色山野 2025-02-15 03:38:38

这将永远不会在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 the connectedCallback; you can't do DOM operations in the constructor (besides shadowDOM content) because the element (this) doesn't exist in the DOM yet.

Also note this._dom isn't required, you get this.shadowRoot for free from attachShadow()

To be clear this.classList is the problem

溺ぐ爱和你が 2025-02-15 03:38:38

好吧, @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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文