HTML DOM insertAdjacentElement() วิธีการแทรกองค์ประกอบในตำแหน่งที่ระบุ
ไวยากรณ์
ต่อไปนี้เป็นไวยากรณ์ -
การเรียก insertAdjacentElement() ด้วยพารามิเตอร์ของ positionString และองค์ประกอบ
node.insertAdjacentElement(“positionString”, element)
สตริงตำแหน่ง
ที่นี่ “positionString” สามารถเป็นดังต่อไปนี้ -
| positionString | คำอธิบาย |
|---|---|
| หลังจากเริ่มต้น | มันแทรกองค์ประกอบหลังจากจุดเริ่มต้นขององค์ประกอบโหนด |
| หลังจบ | มันแทรกองค์ประกอบหลังองค์ประกอบโหนด |
| ก่อนเริ่ม | มันแทรกองค์ประกอบก่อนองค์ประกอบโหนด |
| มาก่อน | มันแทรกองค์ประกอบก่อนจุดสิ้นสุดขององค์ประกอบโหนด |
ตัวอย่าง
ให้เราดูตัวอย่างสำหรับ InsertAdjacentElement() วิธีการ −
<!DOCTYPE html>
<html>
<head>
<title>insertAdjacentElement()</title>
<style>
form {
width:70%;
margin: 0 auto;
text-align: center;
}
* {
padding: 2px;
margin:5px;
}
input[type="button"] {
border-radius: 10px;
}
</style>
</head>
<body>
<form>
<fieldset>
<legend>insertAdjacentElement( )</legend>
<h1>Family Tree</h1>
<span id="Father">Father --></span>
<span id="GrandFather">Grand Father --></span>
<span id="Myself">Myself</span>
<input type="button" onclick="rectifyTree()" value="Correct Family Tree">
</fieldset>
</form>
<script>
function rectifyTree() {
var FSpan = document.getElementById("Father");
var GFSpan = document.getElementById("GrandFather");
GFSpan.insertAdjacentElement("afterend", FSpan);
}
</script>
</body>
</html> ผลลัพธ์
สิ่งนี้จะสร้างผลลัพธ์ต่อไปนี้ -
ก่อนคลิก 'แก้ไขแผนภูมิต้นไม้ครอบครัว' ปุ่ม −

หลังจากคลิก 'แก้ไขแผนภูมิต้นไม้ครอบครัว' ปุ่ม −
