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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
|
---
title: Element.classList
slug: Web/API/Element/classList
translation_of: Web/API/Element/classList
---
<div>{{APIRef("DOM")}}</div>
<p>Das <code><strong>Element.classList</strong></code> ist eine <code>read-only</code> Eigenschaft, welche die aktuelle {{domxref("DOMTokenList")}} Sammlung der Klassen-Attribute des Elements zurückgibt.</p>
<p>Die Benutzung von <code>classList</code> ist eine angenehme Alternative zum Ansprechen der Klassen eines Elements als die leerzeichengetrennte Zeichenfolge via {{domxref("element.className")}}. </p>
<h2 id="Syntax">Syntax</h2>
<pre class="syntaxbox notranslate">const <var>elementClasses</var> = elementNodeReference.classList;
</pre>
<p><em>elementClasses</em> ist eine <a href="/en-US/docs/DOM/DOMTokenList">DOMTokenList</a>, welche die Klassen-Attribute der <em>elementNodeReference </em>repräsentiert. Wenn das Klassen-Attribut nicht gesetzt wurde oder <em>elementClasses.length </em>leer ist, wird 0 zurückgegeben. <code>Element.classList</code> selbst ist nur lesbar (read-only), obgleich es modifiziert werden kann, indem die Methoden <code>add()</code> und <code>remove()</code> angewendet werden.</p>
<h2 id="Methoden">Methoden</h2>
<dl>
<dt>add( String [, String [, ...]] )</dt>
<dd>Fügt angegebene Klassenwerte hinzu. Wenn diese Klassen bereits im Attribut des Elements vorhanden sind, werden sie ignoriert.</dd>
<dt>remove( String [, String [, ...]] )</dt>
<dd>Ausgewählte Klassenwerte entfernen.<br>
<strong>Bemerkung:</strong> Entfernen eines nicht vorhandenen Klassenwertes wirft keinen Fehler.</dd>
<dt><strong>item</strong> ( Number )</dt>
<dd>Rückgabewert nach Index in der Sammlung.</dd>
<dt><strong>toggle</strong> ( String [, force] )</dt>
<dd>Wenn nur ein Argument vorhanden ist: Klassenwert umschalten; d.h. wenn die Klasse existiert, dann <u>entfernt</u> es diese und gibt <code>false</code> zurück, wenn nicht, dann <u>fügt</u> es diese hinzu und gibt <code>true</code> zurück.</dd>
<dd>Wenn ein zweites Argument vorhanden ist: Wenn das zweite Argument auf <code>true</code> basiert, fügt es den angegebenen Klassenwert hinzu. Wenn es <code>false</code> auswertet, entfernt es ihn.</dd>
<dt>contains( String )</dt>
<dd>Überprüft, ob der angegebene Klassenwert im Klassenattribut des Elements vorhanden ist.</dd>
<dt>replace( oldClass, newClass )</dt>
<dd>Ersetzt einen vorhandenen Klassenwert.</dd>
</dl>
<h2 id="Beispiele">Beispiele</h2>
<pre class="brush: js notranslate">const div = document.createElement('div');
div.className = 'foo';
// Status zum Beginn: <div class="foo"></div>
console.log(div.outerHTML);
// classList-API zum Entfernen und Ergänzen von Klassen nutzen
div.classList.remove("foo");
div.classList.add("anotherclass");
// <div class="anotherclass"></div>
console.log(div.outerHTML);
// Wenn visible gesetzt ist entferne es, sonst füge es hinzu
div.classList.toggle("visible");
// Hinzufügen/Enfernen von visible, abhängig von der Bedingung, ob i kleiner 10 ist
div.classList.toggle("visible", i < 10 );
console.log(div.classList.contains("foo"));
// Mehrere Klassen hinzufügen / entfernen
div.classList.add("foo", "bar", "baz");
div.classList.remove("foo", "bar", "baz");
// Mehrere Klassen mittels Spread-Syntax hinzufügen / entfernen
const cls = ["foo", "bar"];
div.classList.add(...cls);
div.classList.remove(...cls);
// Klasse "foo" durch "bar" ersetzen
div.classList.replace("foo", "bar");</pre>
<div class="note">
<p>Firefox-Versionen vor 26 setzen nicht die Nutzung die Nutzung von mehreren Argumenten in den Methoden add/remove/toggle um. Siehe <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=814014">https://bugzilla.mozilla.org/show_bug.cgi?id=814014</a></p>
</div>
<h2 id="Polyfill">Polyfill</h2>
<pre class="brush:js notranslate">// Source: https://gist.github.com/k-gun/c2ea7c49edf7b757fe9561ba37cb19ca
/**
* Element.prototype.classList for IE8/9, Safari.
* @author Kerem Güneş <k-gun@mail.com>
* @copyright Released under the MIT License <https://opensource.org/licenses/MIT>
* @version 1.2
* @see https://developer.mozilla.org/en-US/docs/Web/API/Element/classList
*/
;(function() {
// Helpers.
var trim = function(s) {
return s.replace(/^\s+|\s+$/g, '');
},
regExp = function(name) {
return new RegExp('(^|\\s+)'+ name +'(\\s+|$)');
},
forEach = function(list, fn, scope) {
for (var i = 0; i < list.length; i++) {
fn.call(scope, list[i]);
}
};
// Class list object with basic methods.
function ClassList(element) {
this.element = element;
}
ClassList.prototype = {
add: function() {
forEach(arguments, function(name) {
if (!this.contains(name)) {
this.element.className = trim(this.element.className +' '+ name);
}
}, this);
},
remove: function() {
forEach(arguments, function(name) {
this.element.className = trim(this.element.className.replace(regExp(name), ' '));
}, this);
},
toggle: function(name) {
return this.contains(name) ? (this.remove(name), false) : (this.add(name), true);
},
contains: function(name) {
return regExp(name).test(this.element.className);
},
item: function(i) {
return this.element.className.split(/\s+/)[i] || null;
},
// bonus
replace: function(oldName, newName) {
this.remove(oldName), this.add(newName);
}
};
// IE8/9, Safari
// Remove this if statements to override native classList.
if (!('classList' in Element.prototype)) {
// Use this if statement to override native classList that does not have for example replace() method.
// See browser compatibility: https://developer.mozilla.org/en-US/docs/Web/API/Element/classList#Browser_compatibility.
// if (!('classList' in Element.prototype) ||
// !('classList' in Element.prototype && Element.prototype.classList.replace)) {
Object.defineProperty(Element.prototype, 'classList', {
get: function() {
return new ClassList(this);
}
});
}
// For others replace() support.
if (window.DOMTokenList && !DOMTokenList.prototype.replace) {
DOMTokenList.prototype.replace = ClassList.prototype.replace;
}
})();
</pre>
<h2 id="Spezifikationen">Spezifikationen</h2>
<table class="standard-table">
<thead>
<tr>
<th scope="col">Spezifikation</th>
<th scope="col">Status</th>
<th scope="col">Kommentar</th>
</tr>
</thead>
<tbody>
<tr>
<td>{{SpecName("HTML WHATWG", "dom.html#dom-classlist", "Element.classList")}}</td>
<td>{{Spec2("HTML WHATWG")}}</td>
<td>Note within the HTML specification related to the {{htmlattrxref("class")}} attribute.</td>
</tr>
<tr>
<td>{{SpecName("DOM WHATWG", "#dom-element-classlist", "Element.classList")}}</td>
<td>{{Spec2("DOM WHATWG")}}</td>
<td>Initial definition</td>
</tr>
<tr>
<td>{{SpecName("DOM4", "#dom-element-classlist", "Element.classList")}}</td>
<td>{{Spec2("DOM4")}}</td>
<td></td>
</tr>
</tbody>
</table>
<h2 id="Browserkompatibilität">Browserkompatibilität</h2>
<div>{{CompatibilityTable}}</div>
<div id="compat-desktop">
<table class="compat-table">
<tbody>
<tr>
<th>Feature</th>
<th>Chrome</th>
<th>Edge</th>
<th>Firefox (Gecko)</th>
<th>Internet Explorer</th>
<th>Opera</th>
<th>Safari (WebKit)</th>
</tr>
<tr>
<td>Basic support</td>
<td>8</td>
<td>12</td>
<td>{{CompatGeckoDesktop(1.9.2)}}</td>
<td>10<sup>[1]</sup></td>
<td>11.50</td>
<td>5.1</td>
</tr>
<tr>
<td><code>toggle()</code> method's second argument</td>
<td>24</td>
<td>12</td>
<td>{{CompatGeckoDesktop(24)}}</td>
<td>{{CompatNo}}<sup>[2]</sup></td>
<td>15</td>
<td>7</td>
</tr>
<tr>
<td>Multiple arguments for <code>add()</code> & <code>remove()</code></td>
<td>28</td>
<td>12</td>
<td>{{CompatGeckoDesktop(26)}}</td>
<td>{{CompatNo}}</td>
<td>15</td>
<td>7</td>
</tr>
<tr>
<td><code>replace()</code></td>
<td>{{CompatNo}}</td>
<td>{{CompatUnknown}}</td>
<td>{{CompatGeckoDesktop("49")}}</td>
<td>{{CompatNo}}</td>
<td>{{CompatNo}}</td>
<td>{{CompatNo}}</td>
</tr>
</tbody>
</table>
</div>
<div id="compat-mobile">
<table class="compat-table">
<tbody>
<tr>
<th>Feature</th>
<th>Android</th>
<th>Edge</th>
<th>Firefox Mobile (Gecko)</th>
<th>IE Mobile</th>
<th>Opera Mobile</th>
<th>Safari Mobile</th>
</tr>
<tr>
<td>Basic support</td>
<td>3.0</td>
<td>12</td>
<td>{{CompatGeckoMobile(1.9.2)}}</td>
<td>10<sup>[1]</sup></td>
<td>11.10</td>
<td>5.0</td>
</tr>
<tr>
<td>toggle method's second argument</td>
<td>4.4</td>
<td>12</td>
<td>{{CompatGeckoMobile(24)}}</td>
<td>{{CompatNo}}<sup>[2]</sup></td>
<td>{{CompatUnknown}}</td>
<td>7.0</td>
</tr>
<tr>
<td>multiple arguments for <code>add()</code> & <code>remove()</code></td>
<td>4.4</td>
<td>12</td>
<td>{{CompatUnknown}}</td>
<td>{{CompatNo}}</td>
<td>{{CompatUnknown}}</td>
<td>7.0</td>
</tr>
<tr>
<td><code>replace()</code></td>
<td>{{CompatNo}}</td>
<td>{{CompatUnknown}}</td>
<td>{{CompatGeckoDesktop("49")}}</td>
<td>{{CompatNo}}</td>
<td>{{CompatNo}}</td>
<td>{{CompatNo}}</td>
</tr>
</tbody>
</table>
</div>
<p>[1] Not supported for SVG elements. See <a href="https://connect.microsoft.com/IE/feedback/details/1046039/classlist-not-working-on-svg-elements">a report at Microsoft about that</a>.<br>
[2] Internet Explorer never implemented this. See <a href="https://connect.microsoft.com/IE/feedback/details/878564/element-classlist-toggle-does-not-support-second-parameter">a report at Microsoft about that</a>.</p>
<h2 id="Siehe_auch">Siehe auch</h2>
<ul>
<li>{{domxref("element.className")}}</li>
<li>{{domxref("DOMTokenList")}}</li>
</ul>
|