-
Notifications
You must be signed in to change notification settings - Fork 8k
/
index.md
57 lines (37 loc) · 1.58 KB
/
index.md
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
---
title: "DOMTokenList: contains() メソッド"
short-title: contains()
slug: Web/API/DOMTokenList/contains
l10n:
sourceCommit: 41a8b9c9832359d445d136b6d7a8a28737badc6b
---
{{APIRef("DOM")}}
**`contains()`** は {{domxref("DOMTokenList")}} インターフェイスのメソッドで、論理値を返します。 `true` は渡されたトークンがそのリストに含まれていることを表し、そうでなければ `false` になります。
## 構文
```js-nolint
contains(token)
```
## 引数
- `token`
- : リストの中に存在するかどうかを調べたいトークンを表す文字列です。
### 返値
論理値で、 `true` は呼び出し元のリストにトークンが含まれていることを表し、そうでなければ `false` になります。
## 例
次の例は、 {{htmlelement("span")}} 要素に設定されたクラスのリストを `DOMTokenList` として返すために {{domxref("Element.classList")}} を使用します。 `"c"` がリスト中に存在することを確認し、その `<span>` の {{domxref("Node.textContent")}} の中に結果を書き込みます。
最初に HTML です。
```html
<span class="a b c"></span>
```
そして JavaScript です。
```js
const span = document.querySelector("span");
span.textContent = span.classList.contains("c")
? "classList に 'c' が含まれています"
: "classList に 'c' が含まれていません";
```
出力結果は以下のようになります。
{{ EmbedLiveSample('Examples', '100%', 60) }}
## 仕様書
{{Specifications}}
## ブラウザーの互換性
{{Compat}}