aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/api/element/toggleattribute/index.html
blob: c98a1882efe74f51e5e4d61bf8dbaeb95647576a (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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
---
title: Element.toggleAttribute()
slug: Web/API/Element/toggleAttribute
tags:
  - API
  - Element
  - 元素
  - 参考
translation_of: Web/API/Element/toggleAttribute
---
<div>{{APIRef("DOM")}}</div>

<p>{{domxref("Element")}} 接口的 <code><strong>toggleAttribute()</strong></code> 方法切换给定元素的某个布尔值属性的状态(如果属性不存在则添加属性,属性存在则移除属性)。</p>

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

<pre class="syntaxbox notranslate"><em>Element</em>.toggleAttribute(<em>name</em> [, <em>force</em>]);
</pre>

<h3 id="参数">参数</h3>

<dl>
 <dt><code>name</code></dt>
 <dd>A {{domxref("DOMString")}} specifying the name of the attribute to be toggled. The attribute name is automatically converted to all lower-case when <code>toggleAttribute()</code> is called on an HTML element in an HTML document.</dd>
 <dt><code>force</code> {{optional_inline}}</dt>
 <dd>A boolean value to determine whether the attribute should be added or removed, no matter whether the attribute is present or not at the moment.</dd>
</dl>

<h3 id="返回值">返回值</h3>

<p><code>true</code> if attribute <strong><code>name</code></strong> is eventually present, and <code>false</code> otherwise.</p>

<h3 id="异常">异常</h3>

<dl>
 <dt><code>InvalidCharacterError</code></dt>
 <dd>The specified attribute <code>name</code> contains one or more characters which are not valid in attribute names.</dd>
</dl>

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

<p>在下面的例子中,<code>toggleAttribute()</code> 被用于切换 {{HTMLElement("input")}}<code>readonly</code> 属性。</p>

<h3 id="HTML">HTML</h3>

<pre class="brush: html notranslate">&lt;input value="text"&gt;
&lt;button&gt;toggleAttribute("readonly")&lt;/button&gt;</pre>

<h3 id="JavaScript">JavaScript</h3>

<pre class="brush:js notranslate">var button = document.querySelector("button");
var input = document.querySelector("input");

button.addEventListener("click", function(){
  input.toggleAttribute("readonly");
});
</pre>

<h3 id="结果">结果</h3>

<p>{{ EmbedLiveSample('Example', '300', '50') }}</p>

<p>{{DOMAttributeMethods}}</p>

<h2 id="Polyfill">Polyfill</h2>

<div class="standardNoteBlock">
<p><strong>译者注</strong>:下面代码中的 <code>void 0</code><code>undefined</code></p>
</div>

<pre class="brush: js notranslate">if (!Element.prototype.toggleAttribute) {
  Element.prototype.toggleAttribute = function(name, force) {
    if(force !== void 0) force = !!force

    if (this.getAttribute(name) !== null) {
      if (force) return true;

      this.removeAttribute(name);
      return false;
    } else {
      if (force === false) return false;

      this.setAttribute(name, "");
      return true;
    }
  };
}
</pre>

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

<table class="standard-table">
 <thead>
  <tr>
   <th scope="col">规范</th>
   <th scope="col">状态</th>
   <th scope="col">备注</th>
  </tr>
 </thead>
 <tbody>
  <tr>
   <td>{{SpecName('DOM WHATWG', '#dom-element-toggleattribute', 'Element.toggleAttribute')}}</td>
   <td>{{Spec2('DOM WHATWG')}}</td>
   <td></td>
  </tr>
 </tbody>
</table>

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



<p>{{Compat("api.Element.toggleAttribute")}}</p>