aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/api/element/hasattribute/index.html
blob: 7d5a72f87c37b92fe3aa1a9288db9314db4021fc (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
---
title: Element.hasAttribute()
slug: Web/API/Element/hasAttribute
translation_of: Web/API/Element/hasAttribute
---
<div>
 {{APIRef}}</div>
<h2 id="Summary" name="Summary">概述</h2>
<p><code>hasAttribute</code> 返回一个布尔值,指示该元素是否包含有指定的属性(attribute)。</p>
<h2 id="Syntax" name="Syntax">语法</h2>
<pre class="syntaxbox"><var>var result</var> = <var>element</var>.hasAttribute(<var>attName</var>);
</pre>
<ul>
 <li><code>result</code> 为返回的布尔值:<code>true</code> 或 <code>false</code></li>
 <li><code>attName</code> 是一个字符串,表示属性的名称。</li>
</ul>
<h2 id="Example" name="Example">例子</h2>
<pre class="brush:js">// 在为属性设置新值前检测该属性是否存在
var d = document.getElementById("div1");

if (d.hasAttribute("align")) {
  d.setAttribute("align", "center");
}</pre>
<h2 id="Notes" name="Notes">备注</h2>
<div>
 {{DOMAttributeMethods}}</div>
<h2 id="Specification" name="Specification">规范</h2>
<ul>
 <li><a href="http://www.w3.org/TR/DOM-Level-2-Core/core.html#ID-ElHasAttr">DOM Level 2 Core: hasAttribute</a></li>
 <li><a href="http://www.w3.org/TR/DOM-Level-3-Core/core.html#ID-ElHasAttr">DOM Level 3 Core: hasAttribute</a></li>
</ul>