aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/api/mutationobserver/index.html
blob: 21cd5d2ffcb1f92714c9c1487e0d4d17bd900963 (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
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
---
title: MutationObserver
slug: Web/API/MutationObserver
tags:
  - API
  - Advanced
  - DOM
  - DOM Reference
  - MutationObserver
  - requestAnimationFrame
  - resize
translation_of: Web/API/MutationObserver
---
<p>{{APIRef("DOM WHATWG")}}</p>

<p>{{domxref("MutationObserver")}}接口提供了监视对DOM树所做更改的能力。它被设计为旧的Mutation Events功能的替代品,该功能是DOM3 Events规范的一部分。</p>

<h2 id="构造函数">构造函数</h2>

<dl>
 <dt>{{domxref("MutationObserver.MutationObserver", "MutationObserver()")}}</dt>
 <dd>创建并返回一个新的 <code>MutationObserver</code> 它会在指定的DOM发生变化时被调用。</dd>
</dl>

<h2 id="Instance_methods" name="Instance_methods">方法</h2>

<dl>
 <dt>{{domxref("MutationObserver.disconnect", "disconnect()")}}</dt>
 <dd><code><font face="Open Sans, arial, x-locale-body, sans-serif"><span style="background-color: #ffffff;">阻止 </span></font>MutationObserver</code> 实例继续接收的通知,直到再次调用其{{domxref("MutationObserver.observe", "observe()")}}方法,该观察者对象包含的回调函数都不会再被调用。</dd>
 <dt>{{domxref("MutationObserver.observe", "observe()")}}</dt>
 <dd>配置<code>MutationObserver</code>在DOM更改匹配给定选项时,通过其回调函数开始接收通知。</dd>
 <dt>{{domxref("MutationObserver.takeRecords", "takeRecords()")}}</dt>
 <dd>从MutationObserver的通知队列中删除所有待处理的通知,并将它们返回到{{domxref("MutationRecord")}}对象的新{{jsxref("Array")}}中。</dd>
</dl>

<h2 id="Mutation_Observer_customize_resize_event_listener_demo">Mutation Observer &amp; customize resize event listener &amp; demo</h2>

<p><a href="https://codepen.io/webgeeker/full/YjrZgg/">https://codepen.io/webgeeker/full/YjrZgg/</a></p>

<h2 id="示例">示例</h2>

<p>以下示例改编自<a href="http://hacks.mozilla.org/2012/05/dom-mutationobserver-reacting-to-dom-changes-without-killing-browser-performance/">这篇博客</a></p>

<pre class="brush: js notranslate">// 选择需要观察变动的节点
const targetNode = document.getElementById('some-id');

// 观察器的配置(需要观察什么变动)
const config = { attributes: true, childList: true, subtree: true };

// 当观察到变动时执行的回调函数
const callback = function(mutationsList, observer) {
    // Use traditional 'for loops' for IE 11
    for(let mutation of mutationsList) {
        if (mutation.type === 'childList') {
            console.log('A child node has been added or removed.');
        }
        else if (mutation.type === 'attributes') {
            console.log('The ' + mutation.attributeName + ' attribute was modified.');
        }
    }
};

// 创建一个观察器实例并传入回调函数
const observer = new MutationObserver(callback);

// 以上述配置开始观察目标节点
observer.observe(targetNode, config);

// 之后,可停止观察
observer.disconnect();
</pre>

<ul>
</ul>

<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', '#mutationobserver', 'MutationObserver')}}</td>
   <td>{{ Spec2('DOM WHATWG') }}</td>
   <td></td>
  </tr>
 </tbody>
</table>

<h2 id="浏览器兼容">浏览器兼容</h2>



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

<h2 id="参见">参见</h2>

<ul>
 <li>{{domxref('PerformanceObserver')}}</li>
 <li>{{domxref('ResizeObserver')}}</li>
 <li>{{domxref('IntersectionObserver')}}</li>
 <li><a href="http://updates.html5rocks.com/2012/02/Detect-DOM-changes-with-Mutation-Observers" rel="freelink">A brief overview</a></li>
 <li><a href="http://hacks.mozilla.org/2012/05/dom-mutationobserver-reacting-to-dom-changes-without-killing-browser-performance/" rel="freelink">A more in-depth discussion</a></li>
 <li><a href="http://www.youtube.com/watch?v=eRZ4pO0gVWw" rel="freelink">A screencast by Chromium developer Rafael Weinstein</a></li>
</ul>