Skip to content

Commit 62386bc

Browse files
authored
Merge pull request #268 from marcellosurdi/article/node-properties-type-tag-and-contents
Node properties: type, tag and contents
2 parents 4a2f711 + 092e8cf commit 62386bc

File tree

11 files changed

+215
-216
lines changed

11 files changed

+215
-216
lines changed

2-ui/1-document/05-basic-dom-node-properties/2-lastchild-nodetype-inline/solution.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
There's a catch here.
1+
C'è un tranello in questo esercizio.
22

3-
At the time of `<script>` execution the last DOM node is exactly `<script>`, because the browser did not process the rest of the page yet.
3+
Al momento dell'esecuzione di `<script>` l'ultimo nodo del DOM è esattamente `<script>`, dal momento che il browser non ha ancora elaborato il resto della pagina.
44

5-
So the result is `1` (element node).
5+
Pertanto il risultato è `1` (nodo elemento).
66

77
```html run height=60
88
<html>

2-ui/1-document/05-basic-dom-node-properties/2-lastchild-nodetype-inline/task.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
importance: 5
1+
importanza: 5
22

33
---
44

5-
# What's in the nodeType?
5+
# Cosa contiene nodeType?
66

7-
What does the script show?
7+
Cosa mostrerà lo script?
88

99
```html
1010
<html>
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
1-
Let's make a loop over `<li>`:
1+
Effettuiamo un ciclo iterativo sugli elementi `<li>`:
22

33
```js
44
for (let li of document.querySelectorAll('li')) {
55
...
66
}
77
```
88

9-
In the loop we need to get the text inside every `li`.
9+
Per ciascuna iterazione abbiamo bisogno di ricavare il testo all'interno di ogni `li`.
1010

11-
We can read the text from the first child node of `li`, that is the text node:
11+
Possiamo leggere il testo dal primo nodo figlio di `li`, che è un nodo di testo:
1212

1313
```js
1414
for (let li of document.querySelectorAll('li')) {
1515
let title = li.firstChild.data;
1616

17-
// title is the text in <li> before any other nodes
17+
// title è il testo nel <li> prima di qualsiasi altro nodo
1818
}
1919
```
2020

21-
Then we can get the number of descendants as `li.getElementsByTagName('li').length`.
21+
A questo punto possiamo ricavare il numero dei discendenti con `li.getElementsByTagName('li').length`.

2-ui/1-document/05-basic-dom-node-properties/2-tree-info/solution.view/index.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,12 @@
4141

4242
<script>
4343
for (let li of document.querySelectorAll('li')) {
44-
// get the title from the text node
44+
// ricava il titolo dal nodo di testo
4545
let title = li.firstChild.data;
4646

47-
title = title.trim(); // remove extra spaces from ends
47+
title = title.trim(); // rimuove gli spazi bianchi all'inizio e alla fine
4848

49-
// get the descendants count
49+
// ottiene il numero dei discendenti
5050
let count = li.getElementsByTagName('li').length;
5151

5252
alert(title + ': ' + count);

2-ui/1-document/05-basic-dom-node-properties/2-tree-info/source.view/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
</ul>
4141

4242
<script>
43-
// ... your code...
43+
// ... il tuo codice ...
4444
</script>
4545

4646
</body>
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
importance: 5
1+
importanza: 5
22

33
---
44

5-
# Count descendants
5+
# Contate i discendenti
66

7-
There's a tree structured as nested `ul/li`.
7+
Abbiamo un alberatura HTML strutturata come un elenco di `ul/li` annidati.
88

9-
Write the code that for each `<li>` shows:
9+
Scrivete il codice che per ogni elemento `<li>` mostri:
1010

11-
1. What's the text inside it (without the subtree)
12-
2. The number of nested `<li>` -- all descendants, including the deeply nested ones.
11+
1. Qual è il testo al suo interno (senza considerare il testo di eventuali sottoelementi).
12+
2. Il numero degli elementi `<li>` annidati -- tutti i discendenti, considerando tutti i livelli di annidamento.
1313

1414
[demo src="solution"]
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
The answer: **`BODY`**.
1+
Risposta: **`BODY`**.
22

33
```html run
44
<script>
@@ -10,8 +10,8 @@ The answer: **`BODY`**.
1010
</script>
1111
```
1212

13-
What's going on step by step:
13+
Vediamo cosa succede passo dopo passo:
1414

15-
1. The content of `<body>` is replaced with the comment. The comment is `<!--BODY-->`, because `body.tagName == "BODY"`. As we remember, `tagName` is always uppercase in HTML.
16-
2. The comment is now the only child node, so we get it in `body.firstChild`.
17-
3. The `data` property of the comment is its contents (inside `<!--...-->`): `"BODY"`.
15+
1. Il contenuto di `<body>` è rimpiazzato con il commento. Il commento è `<!--BODY-->`, poiché `body.tagName == "BODY"`. Abbiamo detto che, `tagName` è sempre maiuscolo in modalità HTML.
16+
2. Il commento è ora l'unico nodo figlio, perciò è il risultato di `body.firstChild`.
17+
3. La proprietà `data` del commento è il suo contenuto (ovvero ciò che è dentro i tag di apertura e chiusura `<!--...-->`): `"BODY"`.
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
importance: 3
1+
importanza: 3
22

33
---
44

5-
# Tag in comment
5+
# Tag nel commento
66

7-
What does this code show?
7+
Cosa mostrerà questo codice?
88

99
```html
1010
<script>
1111
let body = document.body;
1212
1313
body.innerHTML = "<!--" + body.tagName + "-->";
1414
15-
alert( body.firstChild.data ); // what's here?
15+
alert( body.firstChild.data ); // cosa c'è qui?
1616
</script>
1717
```
Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,40 @@
11

2-
We can see which class it belongs by outputting it, like:
2+
Possiamo visualizzare a quale classe appartiene esaminandola in questo modo:
33

44
```js run
55
alert(document); // [object HTMLDocument]
66
```
77

8-
Or:
8+
Oppure:
99

1010
```js run
1111
alert(document.constructor.name); // HTMLDocument
1212
```
1313

14-
So, `document` is an instance of `HTMLDocument` class.
14+
Quindi `document` è un'istanza della classe `HTMLDocument`.
1515

16-
What's its place in the hierarchy?
16+
Qual è il suo posto nella gerarchia DOM?
1717

18-
Yeah, we could browse the specification, but it would be faster to figure out manually.
18+
Certo, potremmo sfogliare la specifica, ma sarebbe più veloce scoprirlo manualmente.
1919

20-
Let's traverse the prototype chain via `__proto__`.
20+
Attraversiamo la catena dei prototipi tramite `__proto__`.
2121

22-
As we know, methods of a class are in the `prototype` of the constructor. For instance, `HTMLDocument.prototype` has methods for documents.
22+
Come sappiamo i metodi di una classe sono nel `prototype` del costruttore. Per esempio `HTMLDocument.prototype` ha i metodi per i documenti.
2323

24-
Also, there's a reference to the constructor function inside the `prototype`:
24+
C'è inoltre un riferimento al costruttore all'interno di `prototype`:
2525

2626
```js run
2727
alert(HTMLDocument.prototype.constructor === HTMLDocument); // true
2828
```
2929

30-
To get a name of the class as a string, we can use `constructor.name`. Let's do it for the whole `document` prototype chain, till class `Node`:
30+
Per ricavare la stringa con il nome della classe possiamo usare `constructor.name`. Facciamolo per l'intera catena prototipale `document` fino alla classe` Node`:
3131

3232
```js run
3333
alert(HTMLDocument.prototype.constructor.name); // HTMLDocument
3434
alert(HTMLDocument.prototype.__proto__.constructor.name); // Document
3535
alert(HTMLDocument.prototype.__proto__.__proto__.constructor.name); // Node
3636
```
3737

38-
That's the hierarchy.
38+
Questa è la gerachia.
3939

40-
We also could examine the object using `console.dir(document)` and see these names by opening `__proto__`. The console takes them from `constructor` internally.
40+
Potremmo anche esaminare l'oggetto usando `console.dir(document)` e visualizzare gli stessi nomi aprendo `__proto__`. La console li ricava internamente da `constructor`.
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
importance: 4
1+
importanza: 4
22

33
---
44

5-
# Where's the "document" in the hierarchy?
5+
# In che punto della gerarchia DOM si trova "document"?
66

7-
Which class does the `document` belong to?
7+
A quale classe appartiene `document`?
88

9-
What's its place in the DOM hierarchy?
9+
Qual è il suo posto nella gerarchia DOM?
1010

11-
Does it inherit from `Node` or `Element`, or maybe `HTMLElement`?
11+
Eredita da `Node`, da `Element` o forse da `HTMLElement`?

0 commit comments

Comments
 (0)