Get the value of the scr attribute of an image:
const nodeMap = document.getElementById("light").attributes;
let value = nodeMap.getNamedItem("src").value;
Try it Yourself »
Get the value of the onclick attribute of a button:
const nodeMap = document.getElementById("myButton");
let value = nodeMap.getNamedItem("onclick").value;
Try it Yourself »
The getNamedItem()
method returns an attribute node from a NamedNodeMap object.
It is easier to use the element.getAttribute() method.
The attributes.setNamedItem() Method
namednodemap.getNamedItem(
nodename)
Parameter | Description |
nodename | Required. The name of the node in the namedNodeMap. |
Type | Description |
A node | The node with the specified name. |
attributes.getNamedItem
is a DOM Level 1 (1998) feature.
It is fully supported in all browsers:
Chrome | Edge | Firefox | Safari | Opera | IE |
Yes | Yes | Yes | Yes | Yes | 9-11 |
截取页面反馈部分,让我们更快修复内容!也可以直接跳过填写反馈内容!