HTML DOM removeAttributeNode()เมธอด
❮ วัตถุธาตุตัวอย่าง
ลบโหนดแอตทริบิวต์คลาสจากองค์ประกอบ <h1>:
var elmnt = document.getElementsByTagName("H1")[0]; // Get the first <h1> element in the document
var attr = elmnt.getAttributeNode("class"); // Get the class attribute node from <h1>
elmnt.removeAttributeNode(attr); // Remove the class attribute node from <h1>
ความหมายและการใช้งาน
วิธีการ removeAttributeNode() จะลบแอตทริบิวต์ที่ระบุออกจากองค์ประกอบ และส่งกลับแอตทริบิวต์ที่ลบออก เป็น วัตถุAttr Node
ความแตกต่างระหว่างเมธอดนี้กับเมธอด removeAttribute()คือเมธอด removeAttribute() จะลบแอตทริบิวต์ที่มีชื่อ ที่ระบุ ในขณะที่เมธอดนี้จะลบ อ็อบเจ็กต์ Attr ที่ระบุ ผลลัพธ์จะเหมือนกัน นอกจากนี้ เมธอด removeAttribute() ไม่มีค่าส่งคืนในขณะที่เมธอดนี้ส่งคืนแอตทริบิวต์ที่ลบออก เป็นอ็อบเจ็กต์ Attr
เคล็ดลับ:ใช้ เมธอด getAttributeNode()เพื่อส่งคืนโหนดแอตทริบิวต์ขององค์ประกอบ
เคล็ดลับ:ใช้ เมธอด setAttributeNode()เพื่อเพิ่มโหนดแอตทริบิวต์ให้กับองค์ประกอบ
รองรับเบราว์เซอร์
Method | |||||
---|---|---|---|---|---|
removeAttributeNode() | Yes | Yes | Yes | Yes | Yes |
ไวยากรณ์
element.removeAttributeNode(attributenode)
ค่าพารามิเตอร์
Parameter | Type | Description |
---|---|---|
attributenode | Attr object | Required. The attribute node you want to remove |
รายละเอียดทางเทคนิค
คืนมูลค่า: | วัตถุ Attr แทนโหนดแอตทริบิวต์ที่ถูกลบออก |
---|---|
เวอร์ชัน DOM | วัตถุองค์ประกอบหลักระดับ 1 |
ตัวอย่างเพิ่มเติม
ตัวอย่าง
ลบโหนดแอตทริบิวต์ href ออกจากองค์ประกอบ <a>:
var elmnt = document.getElementById("myAnchor"); // Get the <a> element with id="myAnchor"
var attr = elmnt.getAttributeNode("href"); // Get the href attribute node from <a>
elmnt.removeAttributeNode(attr); // Remove the href attribute node from <a>
หน้าที่เกี่ยวข้อง
บทช่วยสอน HTML: แอตทริบิวต์ HTML
การอ้างอิง HTML DOM: HTML DOM Attribute Object
การอ้างอิง HTML DOM: removeAttribute() Method
การอ้างอิง HTML DOM: getAttributeNode() Method
การอ้างอิง HTML DOM: setAttributeNode() Method
❮ วัตถุธาตุ