HTML DOM метод insertAdjacentElement()
Пример
Переместите элемент span после элемента заголовка:
var s = document.getElementById("mySpan");
var h = document.getElementById("myH2");
h.insertAdjacentElement("afterend",
s);
Определение и использование
Метод insertAdjacentElement()
вставляет указанный элемент в указанную позицию.
Значения юридического положения:
"после начала"
"после окончания"
"до начала"
"до конца"
Поддержка браузера
Цифры в таблице указывают на первую версию браузера, полностью поддерживающую метод.
Method | |||||
---|---|---|---|---|---|
insertAdjacentElement() | Yes | 8 | 48 | Yes | Yes |
Синтаксис
node.insertAdjacentElement(position, element)
Значения параметров
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 |
Дополнительные примеры
Пример
Используя значение «после начала»:
var s = document.getElementById("mySpan");
var h = document.getElementById("myH2");
h.insertAdjacentElement("afterbegin",
s);
Пример
Используя значение «beforebegin»:
var s = document.getElementById("mySpan");
var h = document.getElementById("myH2");
h.insertAdjacentElement("beforebegin",
s);
Пример
Используя значение «до конца»:
var s = document.getElementById("mySpan");
var h = document.getElementById("myH2");
h.insertAdjacentElement("beforeend",
s);