รายการแอตทริบิวต์ HTML DOM ()
ตัวอย่าง
รับชื่อแอตทริบิวต์แรกขององค์ประกอบ:
const nodeMap = document.getElementById("myDiv").attributes;
let name1 = nodeMap.item(0).name;
let name2 = nodeMap.item(1).name;
const nodeMap = document.getElementById("myDiv").attributes;
let name1 = nodeMap[0].name;
let name2 = nodeMap[1].name;
ตัวอย่างเพิ่มเติมด้านล่าง
ความหมายและการใช้งาน
เมธอดส่งคืน แอตทริบิวต์item()
(ตามดัชนี) จาก NamedNodeMap
บันทึก
โหนดจะถูกจัดเรียงตามที่ปรากฏในซอร์สโค้ด ดัชนีเริ่มต้นที่ 0
ดูสิ่งนี้ด้วย:
ไวยากรณ์
namednodemap.item(index)
or simply:
namednodemap[index]
พารามิเตอร์
Parameter | Description |
index | Required. The index of the attribute node in the NamedNodeMap. |
คืนมูลค่า
พิมพ์ | คำอธิบาย |
โหนด | โหนดแอตทริบิวต์ที่ดัชนีที่ระบุ หรือ null ถ้าเลขดัชนีอยู่นอกช่วง |
ตัวอย่างเพิ่มเติม
เปลี่ยนคลาส (สี) ขององค์ประกอบ:
document.getElementById("myDiv").attributes.item(1).value = "class2";
เปลี่ยนคลาส (สี) ขององค์ประกอบ:
document.getElementById("myDiv").attributes[1].value = "class2";
รองรับเบราว์เซอร์
attributes.item()
เป็นคุณลักษณะ DOM ระดับ 1 (1998)
ได้รับการสนับสนุนอย่างเต็มที่ในเบราว์เซอร์ทั้งหมด:
Chrome | IE | Edge | Firefox | Safari | Opera |
Yes | 9-11 | Yes | Yes | Yes | Yes |