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

198
Visualizações
CSS descendent selector to get all elements with a certain attribute, ignoring any that are descendents of an element with that same attribute

I'm using element attributes to identify a tree-like structure in the DOM and each node that branches looks after its children. It does so by searching descendents of the node in the DOM and checking for the attribute that identifies another node. It needs to do this since the child nodes might be several layers deeper in the DOM.

I'm currently doing this with the following JS:

const directChildNodes = [...nodeElement.querySelectorAll(`[tree-node-name="${this.treeName}"]`)]
  // filter all results where the current node is the closest parent
  .filter((el: Element) =>
    el.parentElement?.closest(`[tree-node-name="${this.treeName}"]`) === this.host.nativeElement);

This works, but is not very efficient as it grabs all elements with the attribute and then filters out ones that don't identify the current node as the closest parent. I would ideally be able to achieve this in the CSS selector.

I've tried the following selector but this does not seem to work:

nodeElement.querySelectorAll(
  `[tree-node-name="${this.treeName}"]:not([tree-node-name="${this.treeName}"] [tree-node-name="${this.treeName}"])`
)]

Any suggestions would be great, thanks!

about 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

What about using the :scope pseudo class:

nodeElement.querySelectorAll(':scope > [tree-node-name="${this.treeName}"]')

It's not supported in IE but it's supported in most browsers.

about 4 years ago · Santiago Trujillo 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