aboutsummaryrefslogtreecommitdiff
path: root/files/zh-tw/web/api/mutationobserver/index.html
blob: e2e1e50bfdd35275a1fb909b2392d2b49767f3ff (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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
---
title: MutationObserver
slug: Web/API/MutationObserver
translation_of: Web/API/MutationObserver
---
<p>{{APIRef("DOM")}}</p>

<p><code>MutationObserver</code> 提供開發人員一個方法,來對 <a href="/en-US/docs/DOM">DOM</a> tree 的變動來作反應,這被設計用來替換在 DOM3 事件規範中的 <a href="/en-US/docs/DOM/Mutation_events">Mutation Events</a></p>

<h2 id="建構式">建構式</h2>

<h3 id="MutationObserver()"><code>MutationObserver()</code></h3>

<p>以下舉例為一個新的 DOM mutation observers 建構式。</p>

<pre class="syntaxbox">new MutationObserver(
  function callback
);
</pre>

<h6 id="參數">參數</h6>

<dl>
 <dt><code>callback</code></dt>
 <dd>這個函式會在 DOM 有變化時被呼叫,observer 會用兩個參數來呼叫它,第一個是 <code><a href="#MutationRecord">MutationRecord</a> </code>物件陣列,而第二個參數則是觀察者目標本身。</dd>
</dl>

<h2 id="Instance_methods">Instance methods</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <td><code>void <a href="#observe()">observe</a>( {{domxref("Node")}} target, <a href="#MutationObserverInit">MutationObserverInit</a> options );</code></td>
  </tr>
  <tr>
   <td><code>void <a href="#disconnect()">disconnect</a>();</code></td>
  </tr>
  <tr>
   <td><code>Array <a href="#takeRecords()">takeRecords</a>();</code></td>
  </tr>
 </tbody>
</table>

<div class="note">
<p><strong>筆記</strong>: {{domxref("Node")}} target should not be confused with <a href="https://nodejs.org/en/">NodeJS</a>.</p>
</div>

<h3 id="observe()"><code>observe()</code></h3>

<p>Registers the <code>MutationObserver</code> instance to receive notifications of DOM mutations on the specified node.</p>

<pre class="syntaxbox">void observe(
  {{domxref("Node")}} target,
  <a href="#MutationObserverInit"><code>MutationObserverInit</code></a> options
);
</pre>

<h6 id="Parameters">Parameters</h6>

<dl>
 <dt><code>target</code></dt>
 <dd>The {{domxref("Node")}} on which to observe DOM mutations.</dd>
 <dt><code>options</code></dt>
 <dd>A <a href="#MutationObserverInit"><code>MutationObserverInit</code></a> object, specifies which DOM mutations should be reported.</dd>
</dl>

<div class="note">NOTE: Adding an observer to an element is just like addEventListener, if you observe the element multiple times it does not make a difference. Meaning if you observe element twice, the observe callback does not fire twice, nor will you have to run disconnect() twice. In other words, once an element is observed, observing it again with the same observer instance will do nothing. However if the callback object is different it will of course add another observer to it.</div>

<h3 id="disconnect()"><code>disconnect()</code></h3>

<p>Stops the <code>MutationObserver</code> instance from receiving notifications of DOM mutations. Until the <a href="#observe()"><code>observe()</code></a> method is used again, observer's callback will not be invoked.</p>

<pre class="syntaxbox">void disconnect();
</pre>

<h3 id="takeRecords()"><code>takeRecords()</code></h3>

<p>Empties the <code>MutationObserver</code> instance's record queue and returns what was in there.</p>

<pre class="syntaxbox">Array takeRecords();
</pre>

<h6 id="Return_value">Return value</h6>

<p>Returns an Array of <a href="#MutationRecord"><code>MutationRecord</code>s</a>.</p>

<h2 id="MutationObserverInit"><code>MutationObserverInit</code></h2>

<p><code>MutationObserverInit</code> is an object which can specify the following properties:</p>

<div class="note">NOTE: At the very least, <code>childList</code>, <code>attributes</code>, or <code>characterData</code> must be set to <code>true</code>. Otherwise, "An invalid or illegal string was specified" error is thrown.</div>

<table class="standard-table">
 <tbody>
  <tr>
   <td class="header">Property</td>
   <td class="header">Description</td>
  </tr>
  <tr>
   <td><code>childList</code></td>
   <td>Set to <code>true</code> if additions and removals of the target node's child elements (including text nodes) are to be observed.</td>
  </tr>
  <tr>
   <td><code>attributes</code></td>
   <td>Set to <code>true</code> if mutations to target's attributes are to be observed.</td>
  </tr>
  <tr>
   <td><code>characterData</code></td>
   <td>Set to <code>true</code> if mutations to target's data are to be observed.</td>
  </tr>
  <tr>
   <td><code>subtree</code></td>
   <td>Set to <code>true</code> if mutations to not just target, but also target's descendants are to be observed.</td>
  </tr>
  <tr>
   <td><code>attributeOldValue</code></td>
   <td>Set to <code>true</code> if <code>attributes</code> is set to <code>true</code> and target's attribute value before the mutation needs to be recorded.</td>
  </tr>
  <tr>
   <td><code>characterDataOldValue</code></td>
   <td>Set to <code>true</code> if <code>characterData</code> is set to <code>true</code> and target's data before the mutation needs to be recorded.</td>
  </tr>
  <tr>
   <td><code>attributeFilter</code></td>
   <td>Set to an array of attribute local names (without namespace) if not all attribute mutations need to be observed.</td>
  </tr>
 </tbody>
</table>

<h2 id="MutationRecord"><code>MutationRecord</code></h2>

<p><code>MutationRecord</code> is the object that will be passed to the observer's callback. It has the following properties:</p>

<table class="standard-table">
 <tbody>
  <tr>
   <td class="header">Property</td>
   <td class="header">Type</td>
   <td class="header">Description</td>
  </tr>
  <tr>
   <td><code>type</code></td>
   <td><code>String</code></td>
   <td>Returns <code>attributes</code> if the mutation was an attribute mutation, <code>characterData</code> if it was a mutation to a <code>CharacterData</code> node, and <code>childList</code> if it was a mutation to the tree of nodes.</td>
  </tr>
  <tr>
   <td><code>target</code></td>
   <td><code>{{domxref("Node")}}</code></td>
   <td>Returns the node the mutation affected, depending on the <code>type</code>. For <code>attributes</code>, it is the element whose attribute changed. For <code>characterData</code>, it is the <code>CharacterData</code> node. For <code>childList</code>, it is the node whose children changed.</td>
  </tr>
  <tr>
   <td><code>addedNodes</code></td>
   <td><code>{{domxref("NodeList")}}</code></td>
   <td>Return the nodes added. Will be an empty <code>NodeList</code> if no nodes were added.</td>
  </tr>
  <tr>
   <td><code>removedNodes</code></td>
   <td><code>{{domxref("NodeList")}}</code></td>
   <td>Return the nodes removed. Will be an empty <code>NodeList</code> if no nodes were removed.</td>
  </tr>
  <tr>
   <td><code>previousSibling</code></td>
   <td><code>{{domxref("Node")}}</code></td>
   <td>Return the previous sibling of the added or removed nodes, or <code>null</code>.</td>
  </tr>
  <tr>
   <td><code>nextSibling</code></td>
   <td><code>{{domxref("Node")}}</code></td>
   <td>Return the next sibling of the added or removed nodes, or <code>null</code>.</td>
  </tr>
  <tr>
   <td><code>attributeName</code></td>
   <td><code>String</code></td>
   <td>Returns the local name of the changed attribute, or <code>null</code>.</td>
  </tr>
  <tr>
   <td><code>attributeNamespace</code></td>
   <td><code>String</code></td>
   <td>Returns the namespace of the changed attribute, or <code>null</code>.</td>
  </tr>
  <tr>
   <td><code>oldValue</code></td>
   <td><code>String</code></td>
   <td>The return value depends on the <code>type</code>. For <code>attributes</code>, it is the value of the changed attribute before the change. For <code>characterData</code>, it is the data of the changed node before the change. For <code>childList</code>, it is <code>null</code>.</td>
  </tr>
 </tbody>
</table>

<h2 id="Example_usage">Example usage</h2>

<p>The following example was taken from <a class="external" href="http://hacks.mozilla.org/2012/05/dom-mutationobserver-reacting-to-dom-changes-without-killing-browser-performance/" rel="freelink">this blog post</a>.</p>

<pre class="brush: js">// select the target node
var target = document.querySelector('#some-id');

// create an observer instance
var observer = new MutationObserver(function(mutations) {
  mutations.forEach(function(mutation) {
    console.log(mutation.type);
  });
});

// configuration of the observer:
var config = { attributes: true, childList: true, characterData: true };

// pass in the target node, as well as the observer options
observer.observe(target, config);

// later, you can stop observing
observer.disconnect();
</pre>

<h2 id="Additional_reading">Additional reading</h2>

<ul>
 <li><a class="external" href="http://updates.html5rocks.com/2012/02/Detect-DOM-changes-with-Mutation-Observers" rel="freelink">A brief overview</a></li>
 <li><a class="external" 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 class="external" href="http://www.youtube.com/watch?v=eRZ4pO0gVWw" rel="freelink">A screencast by Chromium developer Rafael Weinstein</a></li>
 <li><a class="external" href="http://code.google.com/p/mutation-summary/" rel="freelink">The mutation summary library</a></li>
 <li><a href="http://dom.spec.whatwg.org/#mutation-observers">The DOM standard</a> which defines the <code>MutationObserver</code> interface</li>
</ul>

<h2 id="Browser_compatibility">Browser compatibility</h2>

<p>{{CompatibilityTable}}</p>

<div id="compat-desktop">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Chrome</th>
   <th>Firefox (Gecko)</th>
   <th>Internet Explorer</th>
   <th>Opera</th>
   <th>Safari</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>18 {{property_prefix("-webkit")}}<br>
    26</td>
   <td>{{CompatGeckoDesktop(14)}}</td>
   <td>11</td>
   <td>15</td>
   <td>6.0 {{property_prefix("-webkit")}}</td>
  </tr>
 </tbody>
</table>
</div>

<div id="compat-mobile">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Android</th>
   <th>Chrome for Android</th>
   <th>Firefox Mobile (Gecko)</th>
   <th>IE Phone</th>
   <th>Opera Mobile</th>
   <th>Safari Mobile</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>{{CompatUnknown}}</td>
   <td>18 {{property_prefix("-webkit")}}<br>
    26</td>
   <td>{{CompatGeckoMobile(14)}}</td>
   <td>11 (8.1)</td>
   <td>15</td>
   <td>6 {{property_prefix("-webkit")}}<br>
    7</td>
  </tr>
 </tbody>
</table>
</div>