Learn how to use JavaScript's new Element Traversal property to traverse child elements

Learn how to use JavaScript's new Element Traversal property to traverse child elements

Previously, child elements could be traversed using the childNodes property or firstChild, but it was necessary to determine whether the child element was an ELement element.

Later, W3C defined a new set of attributes for traversal through the Element Traversal specification, which eliminates the need for judgment and is very convenient.

1. ChildNodes attribute traversal

Child elements are usually traversed through the childNodes property, but this property will contain blank nodes, which is very inconvenient if you only want to traverse Element element.

Please see the following code example:

<div class="article">

    <p>Paragraph 1</p>

    <p>Paragraph 2</p>

    <p>Paragraph Three</p>

</div>

 

<script type="text/javascript">

    let childList = document.querySelector(".article").childNodes;

    console.log(childList);

    // Console output:

    // NodeList(7) 1

</script>

Console view effect:

Traverse the child element blank node:

The obtained child elements contain blank text nodes, which are line breaks, indents, spaces, etc.

If you want to traverse the p element in childList , this is often the case in practice.

Then you need to determine whether the child element is of type Element:

let childList = document.querySelector(".article").childNodes;

// Traverse the child elements childList.forEach((item, index) => {

    if (item.nodeType == 1) { // Check if it is of Element type console.log(item);

    }

});

 

// Console output:

// 3 p elements

2. Element series attribute traversal

Element Traversal adds five properties to DOM elements:

  • 1. childElementCount : the number of child elements ( nodeType=1 ).
  • 2. firstElementChilde points to the first child element of type Element .
  • 3. lastElementChilde points to the last child element of type Element .
  • 4. previousElementSibling points to the previous sibling element of type Element .
  • 5. nextElementSibling points to the next sibling element of type Element .

With these new attributes, it is much easier to traverse Element elements.

Still taking the above example:

// Get the first element let currentElement = document.querySelector(".article").firstElementChild;

// Traverse child elements while (currentElement) {

    console.log(currentElement);

    // Get the next element currentElement = currentElement.nextElementSibling;

}

This makes the process more concise.

Currently IE9 and above, as well as all modern browsers, support these properties.

This is the end of this article about using JavaScript to use the new Element Traversal Element Traversal attribute to traverse child elements. For more related content JavaScript please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Element traversal method in js Element Traversal specification

<<:  CSS+HTML to implement Skeleton Screen loading placeholder animation effect (with animation)

>>:  How to prevent hyperlinks from jumping when using a link

Recommend

Methods and steps to upgrade MySql5.x to MySql8.x

Several Differences Between MySQL 5.x and MySQL 8...

Sharing of SVN service backup operation steps

SVN service backup steps 1. Prepare the source se...

Simple implementation method of vue3 source code analysis

Table of contents Preface 🍹Preparation 🍲vue3 usag...

JavaScript navigator.userAgent obtains browser information case explanation

The browser is probably the most familiar tool fo...

Implementation code of short video (douyin) watermark removal tool

Table of contents 1. Get the first link first 2. ...

The grid is your layout plan for the page

<br /> English original: http://desktoppub.a...

Example of using CSS3 to achieve shiny font effect when unlocking an Apple phone

0. Introduction August 18, 2016 Today, I noticed ...

Analysis of rel attribute in HTML

.y { background: url(//img.jbzj.com/images/o_y.pn...

Detailed process of installing and deploying onlyoffice in docker

0. System requirements CPU I5-10400F or above Mem...

SpringBoot integrates Activiti7 implementation code

After the official release of Activiti7, it has f...

MySQL Optimization: Cache Optimization (Continued)

There are caches everywhere inside MySQL. When I ...

MySQL 8.0.19 installation and configuration tutorial under Windows 10

I will be learning MySQL next semester. I didn...

JavaScript implements displaying a drop-down box when the mouse passes over it

This article shares the specific code of JavaScri...