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

103
Visualizações
Nested HTML element not assigning to slot

In the below code I am trying to assign <span slot='test-slot'>b</span> to <slot name='test-slot'>a</slot> but the assignment does not work. If I bring <span slot='test-slot'>b</span> outside of its parent <div> container the assignment does take place as expected.

Why is this? Is there anyway you can assign from nested elements with the slot element? If not, any alternatives? This is obviously a reduced test case but in my real web component, it is much more intuitive for a user to add an element with the slot tag within other containers.

<test-element>
  <div>
    <span slot='test-slot'>b</span>
  </div>
</test-element>

<template id='template-test-element'>
    <slot name='test-slot'>non slotted content</slot>
</template>

<script>
  class TestElement extends HTMLElement {
    constructor() {
      let template = document.getElementById("template-test-element")
                             .content.cloneNode(true);
      // Initialise shadow root and attach table template
      super() // sets AND return 'this' scope
        .attachShadow({mode:"open"}) // sets AND returns shadowRoot
        .append(template);
    }
  }

  customElements.define('test-element', TestElement);
</script>

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

0

Named slots only accept top-level children that have a matching slot-attribute.

See this (old) Polymer explainer or this more recent article.

Edit: Not sure where this is coming from though, the spec fails to mention this requirement: https://html.spec.whatwg.org/multipage/dom.html#attr-slot

It also is neither mentioned here nor here.

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