Metode penyisipan HTML DOMAdjacentHTML()
Contoh
Masukkan elemen <p> baru setelah elemen header:
var h = document.getElementById("myH2");
h.insertAdjacentHTML("afterend",
"<p>My new paragraph</p>");
Definisi dan Penggunaan
Metode insertAdjacentHTML()
ini menyisipkan teks sebagai HTML, ke posisi tertentu.
Nilai kedudukan hukum adalah:
"sesudah
mulai" " setelah akhir" "sebelum
mulai"
"sebelum akhir"
Dukungan Peramban
Angka-angka dalam tabel menentukan versi browser pertama yang sepenuhnya mendukung metode ini.
Method | |||||
---|---|---|---|---|---|
insertAdjacentHTML() | 1 | 4 | 8 | 4 | 7 |
Sintaksis
node.insertAdjacentHTML(position, text)
Nilai Parameter
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 |
Lebih Banyak Contoh
Contoh
Menggunakan nilai "afterbegin":
h.insertAdjacentHTML("afterbegin", "<span style='color:red'>My span</span>");
Contoh
Menggunakan nilai "beforebegin":
h.insertAdjacentHTML("beforebegin", "<span style='color:red'>My span</span>");
Contoh
Menggunakan nilai "sebelumnya":
h.insertAdjacentHTML("beforeend", "<span style='color:red'>My span</span>");