{"id":824,"date":"2024-07-31T07:35:08","date_gmt":"2024-07-31T05:35:08","guid":{"rendered":"https:\/\/blog.sutilweb.eu\/?page_id=824"},"modified":"2024-07-31T07:35:09","modified_gmt":"2024-07-31T05:35:09","slug":"008-dom-traversing-recorriendo-el-dom","status":"publish","type":"page","link":"https:\/\/sutilweb.eu\/index.php\/lenguajes\/javascript\/javascript-practico\/13-dom-document-object-model\/008-dom-traversing-recorriendo-el-dom\/","title":{"rendered":"008. DOM Traversing: Recorriendo el DOM"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">El <strong>DOM Traversing<\/strong> son una serie de <strong>propiedades<\/strong> que nos da el <strong>API del DOM<\/strong> para, tomando como referencia un <strong>nodo<\/strong>, poder recorrer diferentes <strong>elementos<\/strong>.<\/p>\n\n\n\n<!--more-->\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\"><strong>Nota<\/strong>: todos los <strong>m\u00e9todos<\/strong> que vamos a ver son para los <strong>elementos<\/strong>.<\/p>\n<\/blockquote>\n\n\n\n<p class=\"wp-block-paragraph\">Como vimos en cap\u00edtulos anteriores, hay diferentes tipos de <strong>nodos<\/strong>, podemos verlos pinchando <a href=\"https:\/\/developer.mozilla.org\/es\/docs\/Web\/API\/Node\/nodeType\" target=\"_blank\" rel=\"noreferrer noopener\"><strong>aqu\u00ed<\/strong><\/a>. Recordamos que hay 12 tipos de <strong>nodos<\/strong> diferentes, uno de ellos son los <strong>elementos<\/strong>, que es lo que hemos estado viendo hasta ahora, que son las <strong>etiquetas HTML<\/strong>, tenemos <strong>nodos de texto<\/strong>, <strong>nodos de comentarios<\/strong>, <strong>nodos de fragmentos<\/strong>&#8230;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">El <strong>DOM Traversing<\/strong> que vamos a ver es el enfocado a los <strong>elementos<\/strong>, es decir, a las <strong>etiquetas HTML<\/strong> como tal.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Ejm<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Vamos a trabajar sobre un ejm para entender el funcionamiento.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;!DOCTYPE html&gt;\n&lt;html lang=\"en\"&gt;\n&nbsp; &lt;head&gt;\n&nbsp; &nbsp; &lt;meta charset=\"UTF-8\" \/&gt;\n&nbsp; &nbsp; &lt;meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\" \/&gt;\n&nbsp; &nbsp; &lt;title&gt;DOM Traversing&lt;\/title&gt;\n&nbsp; &nbsp; &lt;style&gt;\n&nbsp; &nbsp; &nbsp; :root {\n&nbsp; &nbsp; &nbsp; &nbsp; --yellow-color: #f7df1e;\n&nbsp; &nbsp; &nbsp; &nbsp; --dark-color: #222;\n&nbsp; &nbsp; &nbsp; }\n&nbsp; &nbsp; &nbsp; .cards {\n&nbsp; &nbsp; &nbsp; &nbsp; border: thin solid var(--dark-color);\n&nbsp; &nbsp; &nbsp; &nbsp; padding: 1rem;\n&nbsp; &nbsp; &nbsp; }\n&nbsp; &nbsp; &nbsp; .card {\n&nbsp; &nbsp; &nbsp; &nbsp; display: inline-block;\n&nbsp; &nbsp; &nbsp; &nbsp; background-color: var(--dark-color);\n&nbsp; &nbsp; &nbsp; &nbsp; color: var(--yellow-color);\n&nbsp; &nbsp; &nbsp; }\n&nbsp; &nbsp; &nbsp; .card figcaption {\n&nbsp; &nbsp; &nbsp; &nbsp; padding: 1rem;\n&nbsp; &nbsp; &nbsp; }\n&nbsp; &nbsp; &nbsp; .rotate-45 {\n&nbsp; &nbsp; &nbsp; &nbsp; transform: rotate(45deg);\n&nbsp; &nbsp; &nbsp; }\n&nbsp; &nbsp; &lt;\/style&gt;\n&nbsp; &lt;\/head&gt;\n\n&nbsp; &lt;body&gt;\n&nbsp; &nbsp; &lt;h1&gt;DOM Traversing&lt;\/h1&gt;\n\n&nbsp; &nbsp; &lt;section class=\"cards\"&gt;\n&nbsp; &nbsp; &nbsp; &lt;figure class=\"card\"&gt;\n&nbsp; &nbsp; &nbsp; &nbsp; &lt;img src=\"tech.jpg\" alt=\"Tech\" \/&gt;\n&nbsp; &nbsp; &nbsp; &nbsp; &lt;figcaption&gt;Tech&lt;\/figcaption&gt;\n&nbsp; &nbsp; &nbsp; &lt;\/figure&gt;\n&nbsp; &nbsp; &nbsp; &lt;figure class=\"card\"&gt;\n&nbsp; &nbsp; &nbsp; &nbsp; &lt;img src=\"animals.avif\" alt=\"Animals\" \/&gt;\n&nbsp; &nbsp; &nbsp; &nbsp; &lt;figcaption&gt;Animals&lt;\/figcaption&gt;\n&nbsp; &nbsp; &nbsp; &lt;\/figure&gt;\n&nbsp; &nbsp; &nbsp; &lt;figure class=\"card\"&gt;\n&nbsp; &nbsp; &nbsp; &nbsp; &lt;img src=\"people.avif\" alt=\"People\" \/&gt;\n&nbsp; &nbsp; &nbsp; &nbsp; &lt;figcaption&gt;People&lt;\/figcaption&gt;\n&nbsp; &nbsp; &nbsp; &lt;\/figure&gt;\n&nbsp; &nbsp; &nbsp; &lt;figure class=\"card\"&gt;\n&nbsp; &nbsp; &nbsp; &nbsp; &lt;img src=\"arch.webp\" alt=\"Arch\" \/&gt;\n&nbsp; &nbsp; &nbsp; &nbsp; &lt;figcaption&gt;Arch&lt;\/figcaption&gt;\n&nbsp; &nbsp; &nbsp; &lt;\/figure&gt;\n&nbsp; &nbsp; &nbsp; &lt;figure class=\"card\"&gt;\n&nbsp; &nbsp; &nbsp; &nbsp; &lt;img src=\"nature.jpg\" alt=\"Nature\" \/&gt;\n&nbsp; &nbsp; &nbsp; &nbsp; &lt;figcaption&gt;Nature&lt;\/figcaption&gt;\n&nbsp; &nbsp; &nbsp; &lt;\/figure&gt;\n&nbsp; &nbsp; &lt;\/section&gt;\n\n&nbsp; &nbsp; &lt;script&gt;\n&nbsp; &nbsp; &nbsp; const $cards = document.querySelector(\".cards\");\n&nbsp; &nbsp; &nbsp; &nbsp;\/\/ Hijos de la clase .cards\n&nbsp; &nbsp; &nbsp; &nbsp;\/\/ Devuelve un tipo de dato HTMLCollection console.log($cards.children);\n&nbsp; &nbsp; &nbsp; &nbsp;\/\/ Acceder a un hijo en particular (el tercero) console.log($cards.children[2]);\n&nbsp; &nbsp; &nbsp; &nbsp;\/\/ Padre de la clase .cards console.log($cards.parentElement);\n&nbsp; &nbsp; &nbsp; &nbsp;\/\/ Primer elemento hijo console.log($cards.firstElementChild);\n&nbsp; &nbsp; &nbsp; &nbsp;\/\/ \u00daltimo elemento hijo console.log($cards.lastElementChild);\n&nbsp; &nbsp; &nbsp; &nbsp;\/\/ Hermano que est\u00e1 antes console.log($cards.previousElementSiblint)\n&nbsp; &nbsp; &nbsp; &nbsp;\/\/ Hermano que est\u00e1 despu\u00e9s console.log($cards.nextElementSibling)\n&nbsp; &nbsp; &nbsp; &nbsp;\/\/ Padre m\u00e1s cercano del tipo de selector que pasemos\n&nbsp; &nbsp; &nbsp; &nbsp;\/\/ Mostrar\u00e1 el div m\u00e1s cercano console.log($cards.closest(\"div\"));\n       console.log($cards);\n&nbsp; &nbsp; &lt;\/script&gt;\n&nbsp; &lt;\/body&gt;\n&lt;\/html&gt;<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Resumen<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><em>children<\/em><\/strong>: para ver los elementos hijos.<\/li>\n\n\n\n<li><strong><em>parentElement<\/em><\/strong>: para ver el elemento padre.<\/li>\n\n\n\n<li><strong><em>firstElementChild<\/em><\/strong>: hace referencia al primer elemento hijo<\/li>\n\n\n\n<li><strong><em>lastElementChild<\/em><\/strong>: hace referencia al \u00faltimo elemento hijo<\/li>\n\n\n\n<li><strong><em>previousElementSibling<\/em><\/strong>: hace referencia al hermano que est\u00e1 antes<\/li>\n\n\n\n<li><strong><em>nextElementSibling<\/em><\/strong>: hace referencia al heramno que est\u00e1 despu\u00e9s<\/li>\n\n\n\n<li><strong><em>closest(\u00abselector\u00bb)<\/em><\/strong>: mostrar\u00e1 el elemento m\u00e1s cercano al tipo de selector que estemos pasando<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>El DOM Traversing son una serie de propiedades que nos da el API del DOM para, tomando como referencia un nodo, poder&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":800,"menu_order":7,"comment_status":"closed","ping_status":"closed","template":"","meta":{"_uag_custom_page_level_css":"","footnotes":""},"class_list":["post-824","page","type-page","status-publish","hentry"],"uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false},"uagb_author_info":{"display_name":"Sutil Web","author_link":"https:\/\/sutilweb.eu\/index.php\/author\/sutilweb\/"},"uagb_comment_info":0,"uagb_excerpt":"El DOM Traversing son una serie de propiedades que nos da el API del DOM para, tomando como referencia un nodo, poder...","_links":{"self":[{"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/pages\/824","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/comments?post=824"}],"version-history":[{"count":2,"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/pages\/824\/revisions"}],"predecessor-version":[{"id":826,"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/pages\/824\/revisions\/826"}],"up":[{"embeddable":true,"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/pages\/800"}],"wp:attachment":[{"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/media?parent=824"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}