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
But we should be very careful about doing it, because what's going on is *not* an addition, but a full overwrite.
247
+
Tuttavia dovremmo stare molto attenti nel fare un'operazione del genere, perché *non* stiamo facendo una semplice aggiunta ma una sovrascrittura completa.
248
248
249
-
Technically, these two lines do the same:
249
+
Tecnicamente queste due righe sono equivalenti:
250
250
251
251
```js
252
252
elem.innerHTML += "...";
253
-
// is a shorter way to write:
253
+
// è un modo più rapido di scrivere:
254
254
*!*
255
255
elem.innerHTML = elem.innerHTML + "..."
256
256
*/!*
257
257
```
258
258
259
-
In other words, `innerHTML+=` does this:
259
+
In altre parole, `innerHTML+=` fa questo:
260
260
261
-
1. The old contents is removed.
262
-
2. The new `innerHTML` is written instead (a concatenation of the old and the new one).
261
+
1. Rimuove il contenuto precedente.
262
+
2. Il nuovo valore di `innerHTML` è inserito al suo posto (una concatenazione del vecchio e del nuovo).
263
263
264
-
**As the content is "zeroed-out" and rewritten from the scratch, all images and other resources will be reloaded**.
264
+
**Poiché il contenuto viene "azzerato" e riscritto da zero, tutte le immagini e le altre risorse verranno ricaricate**.
265
265
266
-
In the `chatDiv` example above the line `chatDiv.innerHTML+="How goes?"` re-creates the HTML content and reloads `smile.gif` (hope it's cached). If `chatDiv` has a lot of other text and images, then the reload becomes clearly visible.
266
+
Nell'esempio `chatDiv` sopra, la linea `chatDiv.innerHTML+="How goes?"` ricrea il contenuto HTML e ricarica `smile.gif` (speriamo sia in cache). Se `chatDiv` ha molto altro testo e immagini, in quel caso, il tempo di ricaricamento potrebbe diventare chiaramente percepibile.
267
267
268
-
There are other side-effects as well. For instance, if the existing text was selected with the mouse, then most browsers will remove the selection upon rewriting `innerHTML`. And if there was an `<input>` with a text entered by the visitor, then the text will be removed. And so on.
268
+
Ci sono anche altri effetti collaterali. Per esempio, se il testo esistente era stato selezionato con il mouse, la maggior parte dei browser rimuoveranno la selezione al momento della riscrittura con `innerHTML`. Se un elemento `<input>` conteneva un testo digitato dal visitatore il testo sarà rimosso, e altri casi simili.
269
269
270
-
Luckily, there are other ways to add HTML besides `innerHTML`, and we'll study them soon.
270
+
Fortunatamente ci sono altri modi di aggiungere HTML oltre che con `innerHTML`, presto li studieremo.
271
271
272
-
## outerHTML: full HTML of the element
272
+
## outerHTML: l'HTML completo di un elemento
273
273
274
-
The `outerHTML` property contains the full HTML of the element. That's like `innerHTML` plus the element itself.
274
+
La proprietà `outerHTML` contiene tutto l'HTML di un elemento. In pratica equivale a `innerHTML` più l'elemento stesso.
275
275
276
-
Here's an example:
276
+
Di seguito un esempio:
277
277
278
278
```html run
279
279
<div id="elem">Hello <b>World</b></div>
@@ -283,11 +283,11 @@ Here's an example:
283
283
</script>
284
284
```
285
285
286
-
**Beware: unlike `innerHTML`, writing to `outerHTML` does not change the element. Instead, it replaces it in the DOM.**
286
+
**Attenzione: diversamente da `innerHTML`, la scrittura in `outerHTML` non cambia l'elemento ma lo sostituisce nel DOM.**
287
287
288
-
Yeah, sounds strange, and strange it is, that's why we make a separate note about it here. Take a look.
288
+
Proprio così, sembra strano, e lo è. Ecco perché ne parliamo subito con una nota a parte. Prestate attenzione.
In the line `(*)` we replaced `div` with `<p>A new element</p>`. In the outer document (the DOM) we can see the new content instead of the `<div>`. But, as we can see in line `(**)`, the value of the old `div` variable hasn't changed!
312
+
Nella linea `(*)` sostituiamo `div` con `<p>A new element</p>`. Nel documento (il DOM) possiamo osservare che il nuovo contenuto ha preso il posto di `<div>`. Tuttavia, come possiamo notare nella linea `(**)`, il precedente valore della variabile `div`non è cambiato!
313
313
314
-
The `outerHTML` assignment does not modify the DOM element (the object referenced by, in this case, the variable 'div'), but removes it from the DOM and inserts the new HTML in its place.
314
+
L'assegnazione con `outerHTML` non cambia l'elemento (cioè l'oggetto a cui fa riferimento, in questo caso, la variabile 'div'), però lo rimuove dal DOM e inserisce il nuovo HTML al suo posto.
315
315
316
-
So what happened in `div.outerHTML=...` is:
317
-
- `div` was removed from the document.
318
-
- Another piece of HTML `<p>A new element</p>` was inserted in its place.
319
-
- `div` still has its old value. The new HTML wasn't saved to any variable.
316
+
Ricapitolando ciò che è successo in `div.outerHTML=...` è:
317
+
- `div` è stato rimosso dal documento.
318
+
- Un pezzo di HTML differente `<p>A new element</p>` è stato inserito al suo posto.
319
+
- `div` mantiene ancora il suo valore precedente. L'HTML inserito in seguito non è stato memorizzato in alcuna variabile.
320
320
321
-
It's so easy to make an error here: modify `div.outerHTML` and then continue to work with `div` as if it had the new content in it. But it doesn't. Such thing is correct for `innerHTML`, but not for `outerHTML`.
321
+
È molto semplice commettere un errore a questo punto: modificare `div.outerHTML` e procedere con `div` come se avesse recepito il nuovo contenuto. Ma questo non avviene. Tale convinzione è corretta per `innerHTML`, ma non per `outerHTML`.
322
322
323
-
We can write to `elem.outerHTML`, but should keep in mind that it doesn't change the element we're writing to ('elem'). It puts the new HTML in its place instead. We can get references to the new elements by querying the DOM.
323
+
Possiamo scrivere tramite `elem.outerHTML`, ma dovremmo tenere bene presente che non cambia l'elemento ('elem') su cui stiamo scrivendo, sostituisce invece il nuovo HTML al suo posto. Per avere un riferimento valido al nuovo elemento dobbiamo interrogare nuovamente il DOM.
324
324
325
-
## nodeValue/data: text node content
325
+
## nodeValue/data: il contenuto testuale del nodo
326
326
327
-
The `innerHTML` property is only valid for element nodes.
327
+
La proprietà `innerHTML` è valida soltanto per i nodi elemento.
328
328
329
329
Other node types, such as text nodes, have their counterpart: `nodeValue` and `data` properties. These two are almost the same for practical use, there are only minor specification differences. So we'll use `data`, because it's shorter.
0 commit comments