Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

196
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda