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/1-browser-environment/article.md
+2-2Lines changed: 2 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -97,8 +97,8 @@ CSSOM specification
97
97
: Describes styles, manipulations with them and their binding to documents, see <https://www.w3.org/TR/cssom-1/>.
98
98
99
99
HTML specification
100
-
: Describes HTML language and also BOM (browser object model) -- various browser functions: `setTimeout`, `alert`, `location` and so on, see <https://html.spec.whatwg.org>.
100
+
: Describes HTML language (tags etc) and also BOM (browser object model) -- various browser functions: `setTimeout`, `alert`, `location` and so on, see <https://html.spec.whatwg.org>. It takes DOM specification and extends it with many additional properties and methods.
101
101
102
102
Now we'll get down to learning DOM, because the document plays the central role in the UI, and working with it is the most complex part.
103
103
104
-
Please note the links above, because there's so many stuff to learn, it's impossible to cover and remember everything. When you'd like to read about a property or a method -- can go <https://developer.mozilla.org/en-US/search>, but looking up the corresponding spec is even better. Longer to read, but will make your fundamental knowledge stronger.
104
+
Please note the links above, because there's so many stuff to learn, it's impossible to cover and remember everything. When you'd like to read about a property or a method -- can go <https://developer.mozilla.org/en-US/search>, but looking up the corresponding spec may be better. Longer to read, but will make your fundamental knowledge stronger.
Copy file name to clipboardExpand all lines: 2-ui/1-document/2-dom-nodes/article.md
+59-29Lines changed: 59 additions & 29 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -69,10 +69,13 @@ var node = {"name":"HTML","nodeType":1,"children":[{"name":"HEAD","nodeType":1,"
69
69
drawHtmlTree(node, 'div.domtree', 690, 210);
70
70
</script>
71
71
72
-
```smart
73
-
From here on, spaces and line-breaks on DOM pictures will only be shown for "space-only" nodes that have no other text.
72
+
```smart header="Starting/ending spaces and line breaks are usually not shown in DOM tools"
73
+
Tools working with DOM usually do not show spaces at start/end of the text and line-breaks between nodes. That's because they are mainly used to decorate HTML, and do not affect (in most cases) how it is shown.
74
+
75
+
On our DOM pictures we'll omit them too where they are not important, to keep things short.
74
76
```
75
77
78
+
76
79
## Autocorrection
77
80
78
81
If the browser encounters malformed HTML, it automatically corrects it when making DOM.
@@ -83,7 +86,7 @@ Like, if the HTML file is a single word `"Hello"`, the browser will wrap it into
83
86
84
87
**While generating DOM, browser automatically processes errors in the document, closes tags and so on.**
85
88
86
-
Such a document:
89
+
Such an "invalid" document:
87
90
88
91
```html no-beautify
89
92
<p>Hello
@@ -92,14 +95,14 @@ Such a document:
92
95
<li>Dad
93
96
```
94
97
95
-
...Will make a respectable DOM, as the browser knows how to read tags (from the spec):
98
+
...Will become a normal DOM, as the browser read tags and restores the missing parts:
96
99
97
100
<divclass="domtree"></div>
98
101
99
102
<script>
100
103
var node = {"name":"HTML","nodeType":1,"children":[{"name":"HEAD","nodeType":1,"children":[]},{"name":"BODY","nodeType":1,"children":[{"name":"P","nodeType":1,"children":[{"name":"#text","nodeType":3,"content":"Hello"}]},{"name":"LI","nodeType":1,"children":[{"name":"#text","nodeType":3,"content":"Mom"}]},{"name":"LI","nodeType":1,"children":[{"name":"#text","nodeType":3,"content":"and"}]},{"name":"LI","nodeType":1,"children":[{"name":"#text","nodeType":3,"content":"Dad"}]}]}]}
101
104
102
-
drawHtmlTree(node, 'div.domtree', 690, 400);
105
+
drawHtmlTree(node, 'div.domtree', 690, 360);
103
106
</script>
104
107
105
108
````warn header="Tables always have `<tbody>`"
@@ -120,7 +123,7 @@ var node = {"name":"TABLE","nodeType":1,"children":[{"name":"TBODY","nodeType":1
120
123
drawHtmlTree(node, 'div.domtree', 600, 200);
121
124
</script>
122
125
123
-
Do you see? The `<tbody>` has appeared out of nowhere. Should keep in mind while working with tables to evade surprises.
126
+
You see? The `<tbody>` has appeared out of nowhere. Should keep in mind while working with tables to evade surprises.
124
127
````
125
128
126
129
## Other node types
@@ -155,49 +158,76 @@ Here we see a new tree node type -- *comment node*.
155
158
156
159
We may think -- why a comment is added to the DOM? It doesn't affect the visual representation anyway. But there's a rule -- if something's in HTML, then it also must be in the DOM tree.
157
160
158
-
**Everything in HTML has its place in DOM.**
161
+
**Everything in HTML, even comments, becomes a part of DOM.**
159
162
160
-
Even the `<!DOCTYPE...>` directive at the very beginning of HTML is also a DOM node. It's in the DOM tree right before `<html>`. The pictures above don't show that fact, because we are not going to touch that node, but it's there.
163
+
Even the `<!DOCTYPE...>` directive at the very beginning of HTML is also a DOM node. It's in the DOM tree right before `<html>`. We are not going to touch that node, but it's there.
161
164
162
165
The `document` object that represents the whole document is, formally, a DOM node as well.
163
166
164
-
There are 12 node types. In practice we only work with 4 of them:
167
+
There are [12 node types](https://dom.spec.whatwg.org/#node). In practice we mainly work with 4 of them:
165
168
166
169
1. `document` -- the "entry point" into DOM.
167
170
2. element nodes -- HTML-tags, the tree building blocks.
168
171
3. text nodes -- they contain text.
169
172
4. comments -- sometimes we can put the information there, that won't be shown, but JS can read it from DOM.
170
173
171
-
If you want to explore how DOM changes with the document, please consider the [Live DOM Viewer](http://software.hixie.ch/utilities/js/live-dom-viewer/). Just type in/modify the document, and it will show up DOM at instant.
174
+
## See it yourself
172
175
173
-
## Power of DOM
176
+
To see the DOM structure in real-time, try [Live DOM Viewer](http://software.hixie.ch/utilities/js/live-dom-viewer/). Just type in the document, and it will show up DOM at instant.
174
177
175
-
Why besides nice pictures do we need DOM? To manipulate the page -- read the information from HTML, create and modify elements.
178
+
## In the browser inspector
176
179
177
-
The `<html>` node is accessible as `document.documentElement`, and `<body>` -- as `document.body`.
180
+
Another way to explore DOM is to use browser developer tools. Actually, that's what we use when developing.
178
181
179
-
Then we can do something with the node.
182
+
To do so, open the web-page [elks.html](elks.html), turn on browser developer tools and switch to Elements tab.
So you can see the DOM, click on elements, see the details about them and so on.
189
+
190
+
Please note that the DOM structure show in developer tools is simplified. Text nodes are shown just as text. And there are no "blank" (space only) text nodes at all. That's fine, because most of time we are interested in element nodes.
191
+
192
+
Clicking the <span class="devtools" style="background-position:-328px -124px"></span> button allows to choose a node from the webpage using a mouse (or alike) and "inspect" it. Works great when we have a huge HTML page and would like to see the DOM of a particular place in it.
193
+
194
+
Another way to do it would be just right-clicking on a webpage and selecting "Inspect element" in the context menu.
195
+
196
+

197
+
198
+
The right part has tabs:
199
+
- Styles -- to see CSS applied to the current element rule by rule, including built-in rules (gray). Almost everything can be edited at-place including the dimensions/margins/paddings of the box below.
200
+
- Computed -- to see CSS applied to the element by property: for each property we can see a rule that gives it (including CSS inheritance and such).
201
+
- ...there are other less used tabs as well.
189
202
190
-
...But actually much more.
203
+
The best way to study them is to click around. Again, please note that most values are in-place editable.
191
204
192
-
In the next chapters we're going to learn it.
205
+
## Interaction with console
206
+
207
+
As we explore the DOM, open/close nodes, we also may want to apply Javascript to it. Like get a node and some code on it, to see how it works. There are few tips to travel between nodes in Elements tab and the console.
208
+
209
+
Press `key:Esc` -- it will open console right below the Elements tab.
210
+
211
+
Now, the most recently selected element is available as `$0`, the previous one as `$1` etc.
212
+
213
+
We can run commands on them, like `$0.style.background = 'red'` here:
214
+
215
+

216
+
217
+
From the other side, if we're in console and have a node in a variable, then we can use the command `inspect(node)` to see it in the Elements pane. Or we can just output it and explore "at-place".
218
+
219
+

220
+
221
+
From the next chapter on we'll study how to access and modify the DOM using Javascript. The browser developer tools are the great help in debugging things.
193
222
194
223
## Summary
195
224
196
-
- DOM-модель -- это внутреннее представление HTML-страницы в виде дерева.
197
-
- Все элементы страницы, включая теги, текст, комментарии, являются узлами DOM.
198
-
- У элементов DOM есть свойства и методы, которые позволяют изменять их.
199
-
- IE8- не генерирует пробельные узлы.
225
+
An HTML/XML document are represented inside the browser as the DOM tree.
226
+
227
+
- Tags become element nodes and form the structure.
228
+
- Text becomes text nodes.
229
+
- ...etc, everything in HTML has its place in DOM, even comments.
200
230
201
-
Кстати, DOM-модель используется не только в JavaScript, это известный способ представления XML-документов.
231
+
We can use developer tools to inspect DOM and modify it manually. There's an extensive documentation about Chrome developer tools at <https://developers.google.com/web/tools/chrome-devtools>, but the best way to learn it is to click here and there, see various menus: most options are obvious. And then later, when you know most stuff, read the docs and pick up the rest.
202
232
203
-
В следующих главах мы познакомимся с DOM более плотно.
233
+
DOM nodes have properties and methods that allow to modify them. We'll get down to it in further chapters.
0 commit comments