HTML DOM insertAdjacentHTML() metoda
Příklad
Vložte nový prvek <p> za prvek záhlaví:
var h = document.getElementById("myH2");
h.insertAdjacentHTML("afterend",
"<p>My new paragraph</p>");
Definice a použití
Metoda insertAdjacentHTML()
vloží text jako HTML na zadanou pozici.
Hodnoty právního postavení jsou:
"afterbegin"
"afterend"
"beforebegin"
"před"
Podpora prohlížeče
Čísla v tabulce určují první verzi prohlížeče, která tuto metodu plně podporuje.
Method | |||||
---|---|---|---|---|---|
insertAdjacentHTML() | 1 | 4 | 8 | 4 | 7 |
Syntax
node.insertAdjacentHTML(position, text)
Hodnoty parametrů
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 |
Další příklady
Příklad
Použití hodnoty „afterbegin“:
h.insertAdjacentHTML("afterbegin", "<span style='color:red'>My span</span>");
Příklad
Použití hodnoty "beforebegin":
h.insertAdjacentHTML("beforebegin", "<span style='color:red'>My span</span>");
Příklad
Použití hodnoty „před“:
h.insertAdjacentHTML("beforeend", "<span style='color:red'>My span</span>");