Javascript ได้จัดเตรียม insertAdjacentHTML() วิธีการแทรกข้อความเป็น HTML ในตำแหน่งที่ระบุ ตำแหน่งทางกฎหมายเหล่านั้นคือ
-
หลังเริ่ม
-
ต่อจากนี้ไป
-
ก่อนเริ่มต้น
-
มาก่อน
ตำแหน่งทางกฎหมายแต่ละตำแหน่งมีความสำคัญเฉพาะของตนเอง มาคุยกันบ้าง
ตัวอย่าง
ในตัวอย่างต่อไปนี้ การใช้ "insertAdjacentHTML() " วิธีการและตำแหน่งทางกฎหมาย "ภายหลัง " มีการวางข้อความใหม่ติดกับส่วนหัวและผลลัพธ์จะแสดงในผลลัพธ์
<html>
<body>
<h2 id="H2">Header</h2>
<script>
var ins = document.getElementById("H2");
ins.insertAdjacentHTML("afterend","<p>Tutorix is the best e-learning platform</p>");
</script>
</body>
</html> ผลลัพธ์
Header
Tutorix is the best e-learning platform
ตัวอย่าง
ในตัวอย่างต่อไปนี้ การใช้ "insertAdjacentHTML() " วิธีการและตำแหน่งทางกฎหมาย "มาก่อน " มีการวางข้อความใหม่ติดกับส่วนหัวและผลลัพธ์จะแสดงในผลลัพธ์
<html>
<body>
<h2 id="H2">Header</h2>
<script>
var ins = document.getElementById("H2");
ins.insertAdjacentHTML("beforeend","<p>Tutorix is the best e-learning platform</p>");
</script>
</body>
</html> ผลลัพธ์
Header
Tutorix is the best e-learning platform