In the article on DOM I read that the element has neighboring elements on the left and on the right, how to understand this? I imagine DOM as a tree, that is, each element has a parent and may have descendants, but I cannot understand what neighbors are, and all the more so since there are right and left neighbors ... Enlighten me on this issue who is not

  • Children of the same parent: on the left - reaching the list of children of the parent to the current element, on the right - after. - Igor

2 answers 2

Let's take the following code as a base:

<div> <ul> <li>1</li> <li>2</li> <li>3</li> </ul> </div> 

To correctly represent a tree, you can look at it not from top to bottom, but from left to right: DIV > UL > LI .
The ul element has a parent div and children of li .
And among li there are neighboring elements: for <li>2</li> there are two neighbors - the left neighbor <li>1</li> , and the right <li>3</li> .

You can read about navigating through these elements with Javascript via the following link: Navigating through DOM elements

  • with <li> I understood, and for UL <DIV, will there be a neighbor on the left? and for UL> LI there will be a neighbor on the right? Do I understand correctly? - perfect
  • one
    @perfect is not, UL no neighbors, lives alone - Igor
  • @Igor and if the <div> has three lists inside? then the second list will have a neighbor left and right? - perfect
  • @perfect - will be - Igor
  • @Igor thanks now everything is clear - perfect

As far as I can tell, the question implies not neighbors as such, but (siblings) descendants from one parent. Graphically, this can be displayed as:

  родитель __________|__________ | | | потомок_1 потомок_2 потомок_3 

where the child_2 , there are two "neighbors" on the left (child_1) and on the right (child_3).