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

259
Visualizações
Accessing child element with JQuery in connectedCallback (Web Compoenent)

I have a very simply Web Compoenent as follows which loads a the compoenent template file from the server:

export default class EditorComponent extends HTMLElement {
    async connectedCallback() {
        let file = "http://blog/components/editor/editor.html";
        let res = await fetch( file );
        this.attachShadow( { mode: 'open' } ).innerHTML = await res.text();
    }
}

customElements.define('editor-component', EditorComponent);

I need to access the child elements in the component HTML file via jQuery. I have tried the following but to no avail:

async connectedCallback() {
    let file = "http://blog/components/editor/editor.html";
    let res = await fetch( file );
    this.attachShadow( { mode: 'open' } ).innerHTML = await res.text();
    $(this).find('div').hide(); <<<<< WHAT I AM ATTEMPTING
}

I Have also added a timeout but that also did not help.

Can this be done?

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

0

You have to do the jQuery syntax yourself

I am doing normal standard JS selectors here
Because I unlearned jQuery selectors (requiring a 80KB+ library) when IE9 was released... in 2011

It is a bit blunt to load HTML, display it in shadowDOM,
and then extract only the content you want. See h1 below

The standard DOMParser() can take a share of the work, faster because no HTML is Rendered,
see alink in:

<web-component></web-component>

  <script>
    customElements.define("web-component", class extends HTMLElement {
      async connectedCallback() {
        let file = "https://load-file.github.io/";
        let html = await (await fetch(file)).text();

        // Browser engine parses HTML to visual (shadow)DOM
        this.attachShadow({mode: 'open'}).innerHTML = html;
        let h1 = this.shadowRoot.querySelector("h1");

        // Browser engine parses HTML in Memory
        let doc = new DOMParser().parseFromString(html, "text/html");
        let alink = doc.querySelector("h2 a");
        alink.target = "blank" ;

        this.shadowRoot.replaceChildren(h1, "SHIFT+Click this link: ",  alink);
      }
    })
  </script>

Read up on all methods in:

  • https://developer.mozilla.org/en-US/docs/Web/API/DocumentFragment
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