Get the value of the class attribute node of an <h1> element:
const element = document.getElementsByTagName("H1")[0];
let text = element.getAttributeNode("class").value;
Try it Yourself »
More examples below.
The getAttributeNode()
method returns an element's attribute.
The getAttributeNode()
method returns an Attribute object.
The getAttribute()
method returns the value of an attribute.
The getAttributeNode()
method returns an Attr object, and you must use the Attr value property to get the value.
The result will be the same.
It is easier to use The getAttribute() Method
The removeAttributeNode() method
element.getAttributeNode(
name)
Parameter | Description |
name | Required. The name of the attribute. |
Type | Description |
Object | The Attr object of the attribute node.null if the attribute does not exist. |
Get the value of the target attribute node of an <a> element:
var elmnt = document.getElementById("myAnchor");
var attr = elmnt.getAttributeNode("target").value;
Try it Yourself »
Get the value of the onclick attribute node of a <button> element:
var elmnt = document.getElementById("myBtn");
var attr = elmnt.getAttributeNode("onclick").value;
Try it Yourself »
element.getAttributeNode()
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 |
截取页面反馈部分,让我们更快修复内容!也可以直接跳过填写反馈内容!