Metodo HTML DOM insertAdjacentElement()
Esempio
Sposta un elemento span dopo l'elemento di intestazione:
var s = document.getElementById("mySpan");
var h = document.getElementById("myH2");
h.insertAdjacentElement("afterend",
s);
Definizione e utilizzo
Il insertAdjacentElement()
metodo inserisce un elemento specificato 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 | |||||
---|---|---|---|---|---|
insertAdjacentElement() | Yes | 8 | 48 | Yes | Yes |
Sintassi
node.insertAdjacentElement(position, element)
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) |
element | HTML Element | The element you want to insert |
Altri esempi
Esempio
Usando il valore "dopo l'inizio":
var s = document.getElementById("mySpan");
var h = document.getElementById("myH2");
h.insertAdjacentElement("afterbegin",
s);
Esempio
Utilizzando il valore "prima di iniziare":
var s = document.getElementById("mySpan");
var h = document.getElementById("myH2");
h.insertAdjacentElement("beforebegin",
s);
Esempio
Usando il valore "prima":
var s = document.getElementById("mySpan");
var h = document.getElementById("myH2");
h.insertAdjacentElement("beforeend",
s);