Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> HTML

เมธอด removeAttributeNode() ของ HTML DOM

เมธอด removeAttributeNode() ของ HTML DOM ใช้สำหรับลบแอตทริบิวต์ที่ระบุออกจากองค์ประกอบที่กำหนดในเอกสาร HTML และคืนค่าแอตทริบิวต์ที่ถูกลบกลับมาในรูปแบบออบเจ็กต์ Attr Node

รูปแบบการใช้งาน (Syntax)

node.removeAttributeNode(attributeNode);

ตัวอย่างการใช้งาน

ตัวอย่างด้านล่างแสดงการใช้งานเมธอด removeAttributeNode()

<!DOCTYPE html> <html> <head> <style>    html{       height:100%;    }    body{       text-align:center;       color:#fff;       background: linear-gradient(62deg, #FBAB7E 0%, #F7CE68 100%) center/cover no-repeat;       height:100%;    }    .btn{       background:#0197F6;       border:none;       height:2rem;       border-radius:2px;       width:35%;       margin:2rem auto;       display:block;       color:#fff;       outline:none;       cursor:pointer;    } </style> </head> <body> <h1>สาธิตเมธอด DOM removeAttributeNode()</h1> <p style="color:#db133a;font-size:1.2rem;">สวัสดี! ผมคือองค์ประกอบ p ใน HTML ที่มีข้อความสุ่มๆ</p> <button onclick="remove()" class="btn">ลบแอตทริบิวต์</button> <script>    function remove() {       var p=document.querySelector("p");       var pAtt=p.getAttributeNode("style");       p.removeAttributeNode(pAtt);    } </script> </body> </html>

ผลลัพธ์

โค้ดด้านบนจะให้ผลลัพธ์ดังนี้

เมธอด removeAttributeNode() ของ HTML DOM

คลิกที่ปุ่ม "ลบแอตทริบิวต์" เพื่อลบแอตทริบิวต์ style ออกจากองค์ประกอบ <p>

เมธอด removeAttributeNode() ของ HTML DOM