The space between them is also a node. This is why there are JS libraries. To provide you with options, such as retrieving siblings.
If the HTML source looked like this:
<ul> <li id="A"></li><li id="B"></li> </ul>
It will work as you expect, because there are no spaces between the li elements.
More recently, two more properties have been introduced, called previousElementSibling and nextElementSibling , which ignore these spaces. It runs on IE9 and later, and other major browsers have been supporting it for a while.
source share