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

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

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

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

node.removeAttribute(attributeName);

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

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

<!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 removeAttribute()</h1> <p style="color:#db133a;font-size:1.2rem;">สวัสดี! ผมคือองค์ประกอบ p ใน HTML ที่มีข้อความสุ่มๆ</p> <button onclick="remove()" class="btn">ลบแอตทริบิวต์</button> <script>    function remove() {       document.querySelector("p").removeAttribute("style");    } </script> </body> </html>

ผลลัพธ์

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

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

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

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