Get the value of the class attribute of an element:
let text = element.getAttribute("class");
Try it Yourself »
Get the value of the target attribute of an <a> element:
let text = myAnchor.getAttribute("target");
Try it Yourself »
More examples below.
The getAttribute()
method returns the value of an element's attribute.
element.getAttribute(
name)
Parameter | Description |
name | Required. The name of the attribute. |
Type | Description |
String | The attribute's value.null if the attribute does not exist. |
Get the value of the onclick attribute of a <button> element:
let text = myButton.getAttribute("onclick");
Try it Yourself »
element.getAttribute
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 |
截取页面反馈部分,让我们更快修复内容!也可以直接跳过填写反馈内容!