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

288
Visualizações
Querying a specific child in a recursive DOM structure

I have a DOM structure like this:

<div class="thing">
  <div class="title"></div>
  <div class="content">
    <div class="thing">
      <div class="title"></div>
      <div class="content">
        <!-- More .thing may be here-->
      </div>
    </div>
  </div>
</div>

In this example, I have a class .thing that has a .title and .content. The contents of .content may be another .thing. The issue I'm having is that given an arbitrary .thing, I want to be able to query its .content and .title without querying anything inside of .content.

One way to do this would be to query the child directly, e.g.

const thing = ... // get some .thing
const title = thing.querySelector(':scope > .title')

The issue with this is that I want to be able to insert an arbitrary number of elements in between .thing and .title. One idea I had was to use the :not pseudo-class:

const title = thing.querySelector('.title:not(.content *)'). 

The theory behind this is that I want a title that isn't a child of content. This works for the top-level title, but doesn't work for nested things. What's the best way to query for this?

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

0

give a data-attribute to the content you specifically want to grab.

Example:

const div = document.querySelector("[data-getter='title']");
console.log(div);
[data-getter="title"] {
  background-color: orange;
}
<div>
  <div data-getter="title">
     <p>random text</p>
  </div>
   <div>2nd text</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