Méthode HTML DOM insertAdjacentHTML()
Exemple
Insérez un nouvel élément <p> après l'élément d'en-tête :
var h = document.getElementById("myH2");
h.insertAdjacentHTML("afterend",
"<p>My new paragraph</p>");
Définition et utilisation
La insertAdjacentHTML()
méthode insère un texte au format HTML, à une position spécifiée.
Les valeurs de la position légale sont :
"afterbegin"
"afterend"
"beforebegin"
"beforeend"
Prise en charge du navigateur
Les nombres dans le tableau indiquent la première version du navigateur qui prend entièrement en charge la méthode.
Method | |||||
---|---|---|---|---|---|
insertAdjacentHTML() | 1 | 4 | 8 | 4 | 7 |
Syntaxe
node.insertAdjacentHTML(position, text)
Valeurs des paramètres
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 |
Plus d'exemples
Exemple
Utilisation de la valeur "afterbegin":
h.insertAdjacentHTML("afterbegin", "<span style='color:red'>My span</span>");
Exemple
Utilisation de la valeur "beforebegin":
h.insertAdjacentHTML("beforebegin", "<span style='color:red'>My span</span>");
Exemple
Utilisation de la valeur "beforeend":
h.insertAdjacentHTML("beforeend", "<span style='color:red'>My span</span>");