Método HTML DOM insertAdjacentHTML()
Exemplo
Insira um novo elemento <p> após o elemento header:
var h = document.getElementById("myH2");
h.insertAdjacentHTML("afterend",
"<p>My new paragraph</p>");
Definição e uso
O insertAdjacentHTML()
método insere um texto como HTML, em uma posição especificada.
Os valores da posição legal são:
"depois de
começar" "depois" "antes
de começar"
"antes de terminar"
Suporte ao navegador
Os números na tabela especificam a primeira versão do navegador que oferece suporte total ao método.
Method | |||||
---|---|---|---|---|---|
insertAdjacentHTML() | 1 | 4 | 8 | 4 | 7 |
Sintaxe
node.insertAdjacentHTML(position, text)
Valores de parâmetro
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 |
Mais exemplos
Exemplo
Usando o valor "afterbegin":
h.insertAdjacentHTML("afterbegin", "<span style='color:red'>My span</span>");
Exemplo
Usando o valor "antes de começar":
h.insertAdjacentHTML("beforebegin", "<span style='color:red'>My span</span>");
Exemplo
Usando o valor "antes":
h.insertAdjacentHTML("beforeend", "<span style='color:red'>My span</span>");