aboutsummaryrefslogtreecommitdiff
path: root/files/ja/web/api/domtokenlist/contains/index.html
blob: 0a4f5b3b6ebbd098a58daaf3166c3db85909bd0c (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
---
title: DOMTokenList.contains()
slug: Web/API/DOMTokenList/contains
tags:
- API
- Contains
- DOM
- DOMTokenList
- Method
- Reference
translation_of: Web/API/DOMTokenList/contains
---
<p>{{APIRef("DOM")}}</p>

<p><code><strong>contains()</strong></code>{{domxref("DOMTokenList")}} インターフェイスのメソッドで、 {{domxref("Boolean")}} を返します。 <code>true</code> は渡された <code><var>token</var></code> がそのリストに含まれていることを表し、そうでなければ <code>false</code> になります。</p>

<h2 id="Syntax">構文</h2>

<pre
  class="brush: js"><var>tokenList</var>.contains(<var>token</var>);</pre>

<h3 id="Parameters">引数</h3>

<dl>
  <dt><code><var>token</var></code></dt>
  <dd>{{domxref("DOMString")}} で、リストの中に存在するかどうかを調べたいトークンを表します。</dd>
</dl>

<h3 id="Return_value">返値</h3>

<p>{{domxref("Boolean")}} で、 <code>true</code> は呼び出し元のリストに
  <code><var>token</var></code> が含まれていることを表し、そうでなければ <code>false</code> になります。</p>

<h2 id="Examples"></h2>

<p>次の例は、 {{htmlelement("span")}} 要素に設定されたクラスのリストを {{domxref("Element.classList")}} を使用して <code>DOMTokenList</code> として返します。 <code>"c"</code> がリスト中に存在することを確認し、その <code>&lt;span&gt;</code>{{domxref("Node.textContent")}} の中に結果を書き込みます。</p>

<p>最初に HTML です。</p>

<pre class="brush: html">&lt;span class="a b c"&gt;&lt;/span&gt;</pre>

<p>そして JavaScript です。</p>

<pre class="brush: js">let span = document.querySelector("span");
let classes = span.classList;
let result = classes.contains("c");
if (result) {
  span.textContent = "The classList contains 'c'";
} else {
   span.textContent = "The classList does not contain 'c'";
}</pre>

<p>出力結果は以下のようになります。</p>

<p>{{ EmbedLiveSample('Examples', '100%', 60) }}</p>

<h2 id="Specifications">仕様書</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-domtokenlist-contains','contains()')}}</td>
      <td>{{Spec2('DOM WHATWG')}}</td>
      <td>初回定義</td>
    </tr>
  </tbody>
</table>

<h2 id="Browser_compatibility">ブラウザーの互換性</h2>

<p>{{Compat("api.DOMTokenList.contains")}}</p>