In web development, the HTML structure is often referred to as a **DOM Tree**. jQuery "Traversing" means "moving through" this tree to find elements based on their relationships to other elements. This is a powerful way to select elements without relying on IDs or classes for every single tag.
To understand traversing, you must understand the four primary relationships in an HTML document:
Consider this simple HTML structure:
In the example above:
<div> is an ancestor of the
<li>.
<ul> is the parent of the
<li>.
<li> tags are siblings because they
share
the same <ul> parent.Traversing allows you to write dynamic and flexible code. For example, if you have a list of products, you can write a single script that says: "When I click *this* buy button, find the *closest* price tag above it and read the value."
In the upcoming lessons, we will explore the three main categories of traversing in detail:
| Category | Example Methods |
|---|---|
| Ancestors | parent(), parents(),
parentsUntil() |
| Descendants | children(), find() |
| Siblings | siblings(), next(), prev() |
| Filtering | first(), last(), eq() |
$("span").parent().fadeOut();).