nextSibling łapie text

0

Mam taką listę:

<dl>
     <dt>opis A</dt>
     <dd>AAA</dd>
     <dt>opis B</dt>
     <dd>BBB</dd>
<dl>

I do tego kod:

const opisy = document.querySelector('dl');
opisy.addEventListener('click', (e) => 
{
    if (e.target.tagName == "DT")
    {
        console.log(e.target.nextSibling);
    }
}

Wydaje się, że kliknięcie na element DT powinno złapać element poniżej, czyli DD. Ale łapie mi jakiś text:
screenshot-20220105130411.png
Dopiero ten kod łapie DD:

e.target.nextSibling.nextSibling.
```

O co chodzi z tym text?
2

nextSibling oprócz html'a łapie też tekst bez znaczników, oraz entery :]

enter.jpg

Jeśli chcesz jedynie same znaczniki to użyj nextElementSibling;

https://developer.mozilla.org/en-US/docs/Web/API/Element/nextElementSibling

const descriptions = document.querySelector('dl');

descriptions.addEventListener('click', (e) => {
    if (e.target.tagName == "DT") {
       console.dir(e.target.nextElementSibling);
    }
});

1 użytkowników online, w tym zalogowanych: 0, gości: 1