aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/api/element/getattributenames/index.html
blob: 6a18abc1abcfb3759fcf3aedea2d7644945280f3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
---
title: Element.getAttributeNames()
slug: Web/API/Element/getAttributeNames
tags:
  - getAttributeNames
translation_of: Web/API/Element/getAttributeNames
---
<p>{{APIRef("DOM")}}</p>

<p><span class="seoSummary"><code>Element.getAttributeNames()</code> 返回一个</span>{{jsxref("Array")}}<span class="seoSummary">,该数组包含指定元素(Element)的所有属性名称,如果该元素不包含任何属性,则返回一个空数组。</span></p>

<p>将 <code>getAttributeNames()</code>{{domxref("Element.getAttribute","getAttribute()")}} 组合使用, 是一种有效替代 {{domxref("Element.attributes")}} 的使用方法.</p>

<h2 id="Syntax" name="Syntax">语法</h2>

<pre class="syntaxbox"><em>let attributeNames</em> = element.getAttributeNames();
</pre>

<h2 id="Example" name="Example">例子</h2>

<pre class="brush:js">// 遍历elements的元素
for(let name of element.getAttributeNames())
{
	let value = element.getAttribute(name);
	console.log(name, value);
}
</pre>

<h2 id="规范">规范</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
  <tr>
   <td>{{SpecName("DOM WHATWG", "#dom-element-getattributenames", "Element.getAttributeNames")}}</td>
   <td>{{Spec2("DOM WHATWG")}}</td>
   <td>Initial definition</td>
  </tr>
 </tbody>
</table>

<h2 id="浏览器兼容性">浏览器兼容性</h2>

{{Compat("api.Element.getAttributeNames")}}