You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: 2-ui/1-document/05-basic-dom-node-properties/article.md
+30-26Lines changed: 30 additions & 26 deletions
Original file line number
Diff line number
Diff line change
@@ -8,40 +8,39 @@ In this chapter we'll see more into what they are and their most used properties
8
8
9
9
## DOM node classes
10
10
11
-
DOM nodes have different properties depending on their class. For instance, element nodes corresponding to tag `<a>`have link-related properties, and those corresponding to `<input>`have input-related and so on.
11
+
DOM nodes have different properties depending on their class. For instance, an element node corresponding to tag `<a>`has link-related properties, and the one corresponding to `<input>`has input-related properties and so on. Text nodes are not the same as element nodes. But there are also common properties and methods between all of them, because all classes of DOM nodes form a single hierarchy.
12
12
13
-
Text nodes are not the same as element nodes: they have properties of their own.
13
+
Each DOM node belongs to the corresponding built-in class.
14
14
15
-
But there are also common properties and methods between all of them, because of the inheritance. Each DOM node belongs to the corresponding built-in class. These classes form a hierarchy.
16
-
17
-
The root is [EventTarget](https://dom.spec.whatwg.org/#eventtarget), that is inherited by [Node](http://dom.spec.whatwg.org/#interface-node), and other DOM nodes inherit from it.
15
+
The root of the hierarchy is [EventTarget](https://dom.spec.whatwg.org/#eventtarget), that is inherited by [Node](http://dom.spec.whatwg.org/#interface-node), and other DOM nodes inherit from it.
18
16
19
17
Here's the picture, explanations to follow:
20
18
21
19

22
20
23
21
The classes are:
24
22
25
-
-[EventTarget](https://dom.spec.whatwg.org/#eventtarget) is the root "abstract" class. Objects of that class are never created. It serves as a base, so that all DOM nodes support so-called "events", we'll study them later.
26
-
-[Node](http://dom.spec.whatwg.org/#interface-node) is also an "abstract" class, serving as a base for DOM nodes. It provides the core tree functionality: `parentNode`, `nextSibling`, `childNodes` and so on (they are getters). Objects of `Node` class are never created. But there are concrete node classes that inherit from it, namely: `Text` for text nodes, `Element` for element nodes and more exotic ones like `Comment` for comment nodes.
27
-
-[Element](http://dom.spec.whatwg.org/#interface-element) is a base class for DOM elements. It provides element-level navigation like `nextElementSibling`, `children` and searching methods like `getElementsByTagName`, `querySelector`. In the browser there may be not only HTML, but also XML and SVG documents. The `Element` class serves as a base for more specific classes: `SVGElement`, `XMLElement` and `HTMLElement`.
28
-
-[HTMLElement](https://html.spec.whatwg.org/multipage/dom.html#htmlelement) is finally the basic class for all HTML elements. It is inherited by various HTML elements:
23
+
-[EventTarget](https://dom.spec.whatwg.org/#eventtarget)-- is the root "abstract" class. Objects of that class are never created. It serves as a base, so that all DOM nodes support so-called "events", we'll study them later.
24
+
-[Node](http://dom.spec.whatwg.org/#interface-node)-- is also an "abstract" class, serving as a base for DOM nodes. It provides the core tree functionality: `parentNode`, `nextSibling`, `childNodes` and so on (they are getters). Objects of `Node` class are never created. But there are concrete node classes that inherit from it, namely: `Text` for text nodes, `Element` for element nodes and more exotic ones like `Comment` for comment nodes.
25
+
-[Element](http://dom.spec.whatwg.org/#interface-element)-- is a base class for DOM elements. It provides element-level navigation like `nextElementSibling`, `children` and searching methods like `getElementsByTagName`, `querySelector`. In the browser there may be not only HTML, but also XML and SVG documents. The `Element` class serves as a base for more specific classes: `SVGElement`, `XMLElement` and `HTMLElement`.
26
+
-[HTMLElement](https://html.spec.whatwg.org/multipage/dom.html#htmlelement)-- is finally the basic class for all HTML elements. It is inherited by various HTML elements:
29
27
-[HTMLInputElement](https://html.spec.whatwg.org/multipage/forms.html#htmlinputelement) -- the class for `<input>` elements,
30
28
-[HTMLBodyElement](https://html.spec.whatwg.org/multipage/semantics.html#htmlbodyelement) -- the class for `<body>` elements,
31
29
-[HTMLAnchorElement](https://html.spec.whatwg.org/multipage/semantics.html#htmlanchorelement) -- the class for `<a>` elements
32
30
- ...and so on, each tag has its own class that may provide specific properties and methods.
33
31
34
32
So, the full set of properties and methods of a given node comes as the result of the inheritance.
35
33
36
-
For instance, `<input>` element is of the [HTMLInputElement](https://html.spec.whatwg.org/multipage/forms.html#htmlinputelement) class:
- It inherits the common HTML element methods (and getters/setters) from `HTMLElement` class.
39
-
- Then it supports common element methods, because `HTMLElement` inherits from `Element`.
40
-
- Then it supports common DOM node properties, because it inherits from `Node`.
41
-
- Finally, it supports events (to be covered), because it inherits from `EventTarget`.
42
-
- (and for completeness: `EventTarget` inherits from `Object`)
34
+
For example, let's consider the DOM object for an `<input>` element. It belongs to [HTMLInputElement](https://html.spec.whatwg.org/multipage/forms.html#htmlinputelement) class. It gets properties and methods as a superposition of:
35
+
36
+
-`HTMLInputElement` -- this class provides input-specific properties, and inherits from...
37
+
-`HTMLElement` -- it provides common HTML element methods (and getters/setters) and inherits from...
38
+
-`Element` -- provides generic element methods and inherits from...
39
+
-`Node` -- provides common DOM node properties and inherits from...
40
+
-`EventTarget` -- gives the support for events (to be covered),
41
+
- ...and finally it inherits from `Object`, so "pure object" methods like `hasOwnProperty` are also available.
43
42
44
-
To see the DOM node class name, we can remember that an object usually has the `constructor` property. It references to the class constructor, so the `constructor.name` is what we need:
43
+
To see the DOM node class name, we can recall that an object usually has the `constructor` property. It references to the class constructor, and `constructor.name` is its name:
As we can see, DOM nodes are regular JavaScript objects. They use prototype-based classes for inheritance. That's easy to see by outputting an element with `console.dir(elem)`. There you can see `HTMLElement.prototype`, `Element.prototype` and so on.
65
+
As we can see, DOM nodes are regular JavaScript objects. They use prototype-based classes for inheritance.
66
+
67
+
That's also easy to see by outputting an element with `console.dir(elem)` in a browser. There in the console you can see `HTMLElement.prototype`, `Element.prototype` and so on.
67
68
68
69
```smart header="`console.dir(elem)` versus `console.log(elem)`"
69
70
Most browsers support two commands in their developer tools: `console.log` and `console.dir`. They output their arguments to the console. For JavaScript objects these commands usually do the same.
@@ -79,19 +80,20 @@ Try it on `document.body`.
79
80
````smart header="IDL in the spec"
80
81
In the specification classes are described using not JavaScript, but a special [Interface description language](https://en.wikipedia.org/wiki/Interface_description_language) (IDL), that is usually easy to understand.
81
82
82
-
The most important difference is that all properties are given with their types. For instance, `DOMString`, `boolean` and so on.
83
+
In IDL all properties are prepended with their types. For instance, `DOMString`, `boolean` and so on.
83
84
84
85
Here's an excerpt from it, with comments:
85
86
86
87
```js
87
88
// Define HTMLInputElement
88
89
*!*
89
-
// The colon means that it inherits from HTMLElement
90
+
// The colon ":" means that HTMLInputElement inherits from HTMLElement
90
91
*/!*
91
92
interface HTMLInputElement: HTMLElement {
93
+
// here go properties and methods of <input> elements
92
94
93
95
*!*
94
-
// "DOMString" means that the property is a string
96
+
// "DOMString" means that these properties are strings
95
97
*/!*
96
98
attribute DOMString accept;
97
99
attribute DOMString alt;
@@ -158,14 +160,14 @@ alert( document.body.tagName ); // BODY
158
160
159
161
Is there any difference between tagName and nodeName?
160
162
161
-
Actually, yes, the difference is reflected in their names, but is indeed a bit subtle.
163
+
Sure, the difference is reflected in their names, but is indeed a bit subtle.
162
164
163
165
- The `tagName` property exists only for `Element` nodes.
164
166
- The `nodeName` is defined for any `Node`:
165
167
- for elements it means the same as `tagName`.
166
168
- for other node types (text, comment etc) it has a string with the node type.
167
169
168
-
So `tagName` can only be used for elements, while `nodeName` can say something about other node types.
170
+
In other words, `tagName` is only supported by element nodes (as it originates from `Element` class), while `nodeName` can say something about other node types.
169
171
170
172
For instance let's compare `tagName` and `nodeName` for the `document` and a comment node:
171
173
@@ -193,7 +195,7 @@ The browser has two modes of processing documents: HTML and XML. Usually the HTM
193
195
194
196
In HTML mode `tagName/nodeName` is always uppercased: it's `BODY` either for `<body>` or `<BoDy>`.
195
197
196
-
In XML mode the case is kept "as is", but it's rarely used.
198
+
In XML mode the case is kept "as is". Nowadays XML mode is rarely used.
197
199
```
198
200
199
201
@@ -287,7 +289,9 @@ Here's an example:
287
289
</script>
288
290
```
289
291
290
-
Unlike `innerHTML`, writing to `outerHTML` does not change the element. Instead, it replaces it as a whole in the outer context. Yeah, sounds strange, and strange it is. Take a look.
292
+
**Beware: unlike `innerHTML`, writing to `outerHTML` does not change the element. Instead, it replaces it as a whole in the outer context.**
293
+
294
+
Yeah, sounds strange, and strange it is, that's why we make a separate note about it here. Take a look.
0 commit comments