Created
December 31, 2020 03:08
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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