blob: 99bdf29e837598bb76ea12c311be9aae74bfa264 (
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: MutationObserver.takeRecords()
slug: Web/API/MutationObserver/takeRecords
translation_of: Web/API/MutationObserver/takeRecords
---
<div>{{APIRef("DOM WHATWG")}}</div>
<p><span class="seoSummary">{{domxref("MutationObserver")}} の <code><strong>takeRecords()</strong></code> メソッドは、検出されたがオブザーバのコールバック関数で処理されていない DOM の変更に一致するすべてのリストを返し、変更キューは空のままにします。</span> 最も一般的な使用例は、オブザーバを切断する直前に保留中の変更記録をすべて取得し、オブザーバを停止する際に保留中の変更を処理できるようにすることです。</p>
<h2 id="構文">構文</h2>
<pre class="syntaxbox notranslate">const <var>mutationRecords</var> = <var>mutationObserver</var>.takeRecords()
</pre>
<h3 id="引数">引数</h3>
<p>なし</p>
<h3 id="戻り値">戻り値</h3>
<p>{{domxref("MutationRecord")}} オブジェクトの配列で、そのそれぞれがドキュメントの DOM ツリーの監視された部分に適用された変更を記述します。</p>
<div class="note">
<p><strong>注:</strong> 発生したものの、オブザーバーのコールバックに伝えられなかった変更キューは、 <code>takeRecords()</code>をコールした後は空のままになります。</p>
</div>
<h2 id="例">例</h2>
<p>この例では、オブザーバを切断する直前に <code>takeRecords()</code>を呼び出して、未伝達の{{domxref("MutationRecord")}}を処理する方法を示します。</p>
<pre class="brush: js; highlight:[12-18] notranslate">const targetNode = document.querySelector("#someElement");
const observerOptions = {
childList: true,
attributes: true
}
const observer = new MutationObserver(callback);
observer.observe(targetNode, observerOptions);
/* ...later, when it's time to stop observing... */
/* handle any still-pending mutations */
let mutations = observer.takeRecords();
observer.disconnect();
if (mutations) {
callback(mutations);
}
</pre>
<p>12-17 行目のコードは、処理されていない変更の記録を取得し、その記録を処理できるようにコールバックを呼び出します。これは、{{domxref("MutationObserver.disconnect", "disconnect()")}} を呼び出してDOMの観測を停止する直前に行われます。</p>
<h2 id="Specifications" name="Specifications">仕様</h2>
<table class="standard-table">
<thead>
<tr>
<th scope="col">Specification</th>
<th scope="col">Status</th>
<th scope="col">Comment</th>
</tr>
</thead>
<tbody>
<tr>
<td>{{SpecName('DOM WHATWG', '#dom-mutationobserver-takerecords', 'MutationObserver.takeRecords()')}}</td>
<td>{{ Spec2('DOM WHATWG') }}</td>
<td></td>
</tr>
</tbody>
</table>
<h2 id="ブラウザ互換性">ブラウザ互換性</h2>
<p>{{Compat("api.MutationObserver.takeRecords")}}</p>
|