HTML DOM insertAdjacentText() วิธีการแทรกสตริงข้อความในตำแหน่งที่ระบุ
ไวยากรณ์
ต่อไปนี้เป็นไวยากรณ์ -
การเรียก insertAdjacentText() ด้วยพารามิเตอร์ของ positionString และ text
node.insertAdjacentText(“positionString”, text)
สตริงตำแหน่ง
ที่นี่ “positionString” สามารถเป็นดังต่อไปนี้ -
| positionString | คำอธิบาย |
|---|---|
| หลังจากเริ่มต้น | แทรกข้อความหลังจุดเริ่มต้นขององค์ประกอบโหนด |
| ภายหลัง | แทรกข้อความหลังองค์ประกอบโหนด |
| ก่อนเริ่ม | แทรกข้อความก่อนองค์ประกอบโหนด |
| ก่อนหน้านั้น | แทรกข้อความก่อนสิ้นสุดองค์ประกอบโหนด |
ตัวอย่าง
ให้เราดูตัวอย่างสำหรับ InsertAdjacentText() วิธีการ −
<!DOCTYPE html>
<html>
<head>
<title>insertAdjacentText()</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>insertAdjacentText( )</legend>
<h1>First Muslim President of India</h1>
<h2 id="president">A.P.J Abdul Kalam</h2>
<input type="button" onclick="rectifyName()" value="Correct Name">
</fieldset>
</form>
<script>
function rectifyName() {
var presidentH2 = document.getElementById("president");
presidentH2.insertAdjacentText("afterbegin", "DR. ");
}
</script>
</body>
</html> ผลลัพธ์
สิ่งนี้จะสร้างผลลัพธ์ต่อไปนี้ -
ก่อนคลิก 'ชื่อที่ถูกต้อง' ปุ่ม −

หลังจากคลิก 'ชื่อที่ถูกต้อง' ปุ่ม −
