This commit is contained in:
Ilya Kantor 2019-04-18 08:46:58 +03:00
parent 1f19fd8a5a
commit a64fb2661b

View file

@ -237,7 +237,12 @@ alert( document.documentElement.parentElement ); // null
In other words, the `documentElement` (`<html>`) is the root node. Formally, it has `document` as its parent. But `document` is not an element node, so `parentNode` returns it and `parentElement` does not. In other words, the `documentElement` (`<html>`) is the root node. Formally, it has `document` as its parent. But `document` is not an element node, so `parentNode` returns it and `parentElement` does not.
Sometimes that matters when we're walking over the chain of parents and call a method on each of them, but `document` doesn't have it, so we exclude it. This loop travels up from an arbitrary element `elem` to `<html>`, but not to the `document`:
```js
while(elem = elem.parentElement) {
alert( elem ); // parent chain till <html>
}
```
```` ````
Let's modify one of the examples above: replace `childNodes` with `children`. Now it shows only elements: Let's modify one of the examples above: replace `childNodes` with `children`. Now it shows only elements: