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

Summary of frequently used commands for Linux file operations

0. New operation: mkdir abc #Create a new folder ...

Detailed explanation of JavaScript error capture

Table of contents 1. Basic usage and logic 2. Fea...

MySQL series 6 users and authorization

Table of contents Tutorial Series 1. User Managem...

The actual process of encapsulating axios in the project

Table of contents Preface Benefits of axios encap...

Simple implementation of Mysql add, delete, modify and query statements

Simple implementation of Mysql add, delete, modif...

Example of how to upload a Docker image to a private repository

The image can be easily pushed directly to the Do...

Introduction to major browsers and their kernels

Trident core: IE, MaxThon, TT, The World, 360, So...

JavaScript to switch multiple pictures

This article shares the specific code of JavaScri...

MySQL 5.6.23 Installation and Configuration Environment Variables Tutorial

This article shares the installation and configur...

MySQL string splitting operation (string interception containing separators)

String extraction without delimiters Question Req...

Detailed explanation of several storage methods of docker containers

Table of contents Written in front Several storag...

Docker completely deletes private library images

First, let’s take a look at the general practices...