Skip to content

Instantly share code, notes, and snippets.

@sergiodxa
Created December 31, 2020 03:08
Show Gist options
  • Save sergiodxa/fc4f479b3719af8d9590d26d4cc5ce92 to your computer and use it in GitHub Desktop.
Save sergiodxa/fc4f479b3719af8d9590d26d4cc5ce92 to your computer and use it in GitHub Desktop.
TIL: Al usar querySelector(All) desde un elemento es posible usar `:scope` para referirse a ese elemento en el selector de CSS, así podemos usar selectores como `>` para solo obtener hijos directos de nuestro elemento padre
/**
* Si tenemos este HTML
* <section id="some-section">
* <article> <h2>Article 1</h2> <article>A nested one</article> </article>
* <article> <h2>Article 2</h2> <article>A nested one</article> </article>
* <article> <h2>Article 3</h2> <article>A nested one</article> </article>
* </section>
*/
const section = document.querySelector("#some-section");
// Haciendo esto obtenemos seis artículos
section.querySelectorAll("article")
// Usando :scope podemos referencias al section y obtener solo tres
section.querySelectorAll(":scope > article")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment