วิธี HTML DOM Element insertAdjacentText()

คำอธิบายและวิธีใช้

insertAdjacentText() วิธีการใส่ข้อความเข้าไปในตำแหน่งที่กำหนด

ตำแหน่งที่ถูกต้อง:

ค่า รายละเอียด
afterbegin หลังจากเริ่มต้นองค์ประกอบ
afterend หลังองค์ประกอบ
beforebegin ก่อนองค์ประกอบ
beforeend ก่อนจบขององค์ประกอบ

ตัวอย่าง

ตัวอย่าง 1

ใส่ข้อความหลังจากตัวอย่าง

const h2 = document.getElementById("myH2");
let text = "My inserted text";
h2.insertAdjacentText("afterend", text);

ลองด้วยตัวเอง

ตัวอย่าง 2

ใช้ "afterbegin":

const h2 = document.getElementById("myH2");
let text = "My inserted text";
h2.insertAdjacentText("afterbegin", text);

ลองด้วยตัวเอง

ตัวอย่าง 3

ใช้ "beforebegin":

h2.insertAdjacentText("beforebegin", text);

ลองด้วยตัวเอง

ตัวอย่าง 4

ใช้ "beforeend":

h2.insertAdjacentText("beforeend", text);

ลองด้วยตัวเอง

คำสั่ง

element.insertAdjacentText(ตำแหน่ง, text)

หรือ

node.insertAdjacentText(ตำแหน่ง, text)

ประกาศ

ประกาศ รายละเอียด
ตำแหน่ง

สำคัญ。

  • afterbegin
  • afterend
  • beforebegin
  • beforeend
text ข้อความที่ต้องการใส่

เบราเซอร์สนับสนุน

ทั้งหมดเบราเซอร์ทุกตัวเน้น element.insertAdjacentText()

Chrome IE Edge Firefox Safari Opera
Chrome IE Edge Firefox Safari Opera
สนับสนุน สนับสนุน สนับสนุน สนับสนุน สนับสนุน สนับสนุน