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

383
Visualizações
querySelectorAll exclude all children

I'm using querySelectorAll and want to exclude all children of a number of elements. Trying this does not work.

document.querySelectorAll(".h:not(.p)").forEach(function(e){
  e.style.color = "red";
});
<div class="h">
  Hello World!
  <p class="p">This is a paragraph.</p>
</div>
<div class="h">
  Hello World!
  <p class="p">This is a paragraph.</p>
</div>
<div class="h">
  Hello World!
  <p class="p">This is a paragraph.</p>
</div>

How can I achieve only selecting parent elements? Does such a selector exist?

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

0

If you give a parent a color, that will be inherited by the children as well, by default. Your code is selecting only the parents, the .hs, as you want - the problem is that the children inherit that style as well.

One option is to iterate over the children or descendant <p>s as well, and change their colors to what you want, so they don't inherit from the parent.

document.querySelectorAll(".h").forEach(function(e){
  e.style.color = "red";
});
document.querySelectorAll(".h .p").forEach(function(e){
  e.style.color = "initial";
});
<div class="h">
  Hello World!
  <p class="p">This is a paragraph.</p>
</div>
<div class="h">
  Hello World!
  <p class="p">This is a paragraph.</p>
</div>
<div class="h">
  Hello World!
  <p class="p">This is a paragraph.</p>
</div>

A nicer looking option would be to give the text to be colored its own element, and then select those elements only:

document.querySelectorAll(".h span").forEach(function(e){
  e.style.color = "red";
});
<div class="h">
  <span>Hello World!</span>
  <p class="p">This is a paragraph.</p>
</div>
<div class="h">
  <span>Hello World!</span>
  <p class="p">This is a paragraph.</p>
</div>
<div class="h">
  <span>Hello World!</span>
  <p class="p">This is a paragraph.</p>
</div>

about 4 years ago · Juan Pablo Isaza Relatório

0

This is only selecting the .h elements:

document.querySelectorAll(".h")

But what are you doing with those elements? You're setting the text color to red:

e.style.color = "red";

So all text within those elements will be red, unless styled otherwise. If the .p elements should be something else, style them as such:

document.querySelectorAll(".h").forEach(function(e){
  e.style.color = "red";
});
.p {
  color: black;
}
<div class="h">
  Hello World!
  <p class="p">This is a paragraph.</p>
</div>
<div class="h">
  Hello World!
  <p class="p">This is a paragraph.</p>
</div>
<div class="h">
  Hello World!
  <p class="p">This is a paragraph.</p>
</div>

Alternatively you might consider re-structuring your HTML. For example:

<div>
  <span class="h">Hello World!</span>
  <p class="p">This is a paragraph.</p>
</div>
<div>
  <span class="h">Hello World!</span>
  <p class="p">This is a paragraph.</p>
</div>
<div>
  <span class="h">Hello World!</span>
  <p class="p">This is a paragraph.</p>
</div>

Then you can specifically target the content you want without having to find ways to "exclude" other content.

about 4 years ago · Juan Pablo Isaza Relatório

0

The color is being inherited in the paragraph

document.querySelectorAll(".h:not(.p)").forEach(function(e){
  e.style.color = "red";
  e.querySelectorAll(".p").forEach(function(elem) {
    elem.style.color = "black";
  });
});
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