English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
insertAdjacentHTML()This method parses the specified text as HTML and inserts the resulting node at the specified position.
This method does not reparse the element being used, so it does not destroy the existing elements within the element.
This avoids additional serialization steps, making it faster than directinnerHTMLFaster operation.
element.insertAdjacentHTML(position, text)
var head = document.getElementsByTagName("h2")[0]; head.insertAdjacentHTML("afterend", "<span>Hello world<"/span>Test See‹/›
The numbers in the table specify the first browser version that fully supports the insertAdjacentHTML() method:
Method | |||||
insertAdjacentHTML() | is | 48 | is | is | 8 |
Parameter | Description |
---|---|
position | Relative position to the element. Legal value:
|
text | String to be parsed as HTML |
Using the "beginbegin" value:
var head = document.getElementsByTagName("h2")[0]; head.insertAdjacentHTML("afterbegin", "<span style=""39;color:red;'>Hello world</span>Test See‹/›
Using the "beforebegin" value:
var head = document.getElementsByTagName("h2")[0]; head.insertAdjacentHTML("beforebegin", "<span style="39;color:red;'>Hello world</span>Test See‹/›
Using the "beforeend" value:
var head = document.getElementsByTagName("h2")[0]; head.insertAdjacentHTML("beforeend", "<span style="39;color:red;'>Hello world</span>Test See‹/›
HTML DOM Reference:element.insertAdjacentElement() method
HTML DOM Reference:element.insertAdjacentText() method
HTML DOM Reference:node.insertBefore() method
HTML DOM Reference:node.appendChild() method