Metodo HTML DOM insertAdjacentHTML()
Esempio
Inserisci un nuovo elemento <p> dopo l'elemento di intestazione:
var h = document.getElementById("myH2");
h.insertAdjacentHTML("afterend",
"<p>My new paragraph</p>");
Definizione e utilizzo
Il insertAdjacentHTML()
metodo inserisce un testo come HTML, in una posizione specificata.
I valori della posizione giuridica sono:
"afterbegin"
"afterend"
"beforebegin"
"beforeend"
Supporto browser
I numeri nella tabella specificano la prima versione del browser che supporta completamente il metodo.
Method | |||||
---|---|---|---|---|---|
insertAdjacentHTML() | 1 | 4 | 8 | 4 | 7 |
Sintassi
node.insertAdjacentHTML(position, text)
Valori dei parametri
Parameter | Type | Description |
---|---|---|
position | String | Required. A position relative to the element. Legal values: "afterbegin" - After the beginning of the element (as the first child) "afterend" - After the element "beforebegin" - Before the element "beforeend" - Before the end of the element (as the last child) |
text | String | The text you want to insert |
Altri esempi
Esempio
Usando il valore "dopo l'inizio":
h.insertAdjacentHTML("afterbegin", "<span style='color:red'>My span</span>");
Esempio
Utilizzando il valore "prima di iniziare":
h.insertAdjacentHTML("beforebegin", "<span style='color:red'>My span</span>");
Esempio
Usando il valore "prima":
h.insertAdjacentHTML("beforeend", "<span style='color:red'>My span</span>");