HTML DOM แทรกAdjacentHTML()วิธี การ
ตัวอย่าง
แทรก <p> องค์ประกอบใหม่หลังองค์ประกอบส่วนหัว:
var h = document.getElementById("myH2");
h.insertAdjacentHTML("afterend",
"<p>My new paragraph</p>");
ความหมายและการใช้งาน
วิธี การinsertAdjacentHTML()
แทรกข้อความเป็น HTML ในตำแหน่งที่ระบุ
ค่าตำแหน่งทางกฎหมายคือ:
" Afterbegin"
"หลังจบ" "beforebegin
"
"ก่อน"
รองรับเบราว์เซอร์
ตัวเลขในตารางระบุเบราว์เซอร์รุ่นแรกที่รองรับวิธีการนี้อย่างเต็มที่
Method | |||||
---|---|---|---|---|---|
insertAdjacentHTML() | 1 | 4 | 8 | 4 | 7 |
ไวยากรณ์
node.insertAdjacentHTML(position, text)
ค่าพารามิเตอร์
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 |
ตัวอย่างเพิ่มเติม
ตัวอย่าง
ใช้ค่า "afterbegin":
h.insertAdjacentHTML("afterbegin", "<span style='color:red'>My span</span>");
ตัวอย่าง
ใช้ค่า "beforebegin":
h.insertAdjacentHTML("beforebegin", "<span style='color:red'>My span</span>");
ตัวอย่าง
ใช้ค่า "ก่อน":
h.insertAdjacentHTML("beforeend", "<span style='color:red'>My span</span>");