Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

133
Visualizações
Custom HTML elements not rendering children

The following code does not render the CategoryElement's children - only the <p> element within the Shadow Root. How do I make the CategoryElement's children render?

<html>
    <head>
    </head>
    <body>
        <script>
            class CategoryElement extends HTMLElement {
                constructor() {
                    super();
                    this.attachShadow({mode: 'open'});
                    this.header = document.createElement('p');
                    this.header.innerText = 'Shadow Header';
                    this.shadowRoot.append(this.header);
                }
            }
            customElements.define('category-element', CategoryElement);

            var elem = document.createElement('category-element');
            document.body.appendChild(elem);

            var outer = document.createElement('p');
            outer.innerText = 'Outer Header';
            elem.appendChild(outer);
        </script>
    </body>
<html>

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

I believe this gets you there, but I'm no shadow dom expert by any stretch of the imagination.

I added .shadowRoot as shown here:

elem.shadowRoot.appendChild(outer);

When I inspect with the dev tools I can see that both <p> are in side the shadow-root element (if that's the correct way to refer to it).

</head>

<body>
  <script>
    class CategoryElement extends HTMLElement {
      constructor() {
        super();
        this.attachShadow({
          mode: 'open'
        });
        this.header = document.createElement('p');
        this.header.innerText = 'Shadow Header';
        this.shadowRoot.append(this.header);
      }
    }
    customElements.define('category-element', CategoryElement);

    var elem = document.createElement('category-element');
    document.body.appendChild(elem);

    var outer = document.createElement('p');
    outer.innerText = 'Outer Header';
    elem.shadowRoot.appendChild(outer);
  </script>
</body>
<html>

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda