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

150
Visualizações
jQuery 3.6.0 can't parse "template" tag

The other day I's working on a project using jQuery 3.6.0 but I found that it can't parse <template> tags.

Refer to the below example.

const temp = $("#main");
console.log(temp.find("div").html()); // this isn't working


const temp2 = $("#main2");
console.log(temp2.find("div").html()); // this is working
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<template id="main">
  <div class="first">
    <span>Hola</span>
  </div>
</template>


<div id="main2" style="display:none;">
  <div class="first">
    <span>Hola</span>
  </div>
</div>

What am I doing wrong here?

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

0

template results in a document fragment in which the contents of the template element is. You have to access it using content.

So what you could do is $(temp[0].content).find('div').html()

But you need to be careful about that MDN: <template>: The Content Template element:

However, the HTMLTemplateElement has a content property, which is a read-only DocumentFragment containing the DOM subtree which the template represents. Note that directly using the value of the content could lead to unexpected behavior, see Avoiding DocumentFragment pitfall section below.

const temp = $("#main");
console.log($(temp[0].content).find('div').html()); // this isn't working


const temp2 = $("#main2");
console.log(temp2.find("div").html()); // this is working
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<template id="main">
  <div class="first">
    <span>Hola</span>
  </div>
</template>


<div id="main2" style="display:none;">
  <div class="first">
    <span>Hola</span>
  </div>
</div>

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