aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/api/mutationobserver/takerecords/index.html
blob: b6ad1c65fcc528fe65867454c5511fff42c26e8a (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
---
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>

<ul>
</ul>

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

<pre class="syntaxbox"><em>mutationRecords</em> = <em>mutationObserver</em>.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]">var targetNode = document.querySelector("#someElement");
var observerOptions = {
  childList: true,
  attributes: true
}

var observer = new MutationObserver(callback);
observer.observe(targetNode, observerOptions);

/* ...later, when it's time to stop observing... */

/* handle any still-pending mutations */

var mutations = observer.takeRecords();

if (mutations) {
  callback(mutations);
}

observer.disconnect();
</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">规范</th>
   <th scope="col">状态</th>
   <th scope="col">批注</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>