Element.getAttributeNames()

Element.getAttributeNames() 返回一个Array,该数组包含指定元素(Element)的所有属性名称,如果该元素不包含任何属性,则返回一个空数组。

将 getAttributeNames()getAttribute() 组合使用, 是一种有效替代 Element.attributes 的使用方法.

语法

let attributeNames = element.getAttributeNames();

例子

// 遍历elements的元素
for(let name of element.getAttributeNames())
{
	let value = element.getAttribute(name);
	console.log(name, value);
}

规范

Specification Status Comment
DOM
Element.getAttributeNames
Living Standard Initial definition

浏览器兼容性

We're converting our compatibility data into a machine-readable JSON format. This compatibility table still uses the old format, because we haven't yet converted the data it contains. Find out how you can help! (en-US)
Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari (WebKit)
Basic support 61 45 (45) 未实现 48 9
Feature Android Webview Chrome for Android Firefox Mobile (Gecko) Firefox OS IE Mobile Opera Mobile Safari Mobile
Basic support 61 61 ? ? ? 48 ?