MutationObserver
方法
takeRecords()
returns a list of all matching DOM changes that have been detected but not yet processed by the observer's callback function, leaving the mutation queue empty.
The most common use case for this is to immediately fetch all pending mutation records immediately prior to disconnecting the observer, so that any pending mutations can be processed when stopping down the observer.
const mutationRecords = mutationObserver.takeRecords()
None.
An array
MutationRecord
objects, each describing one change applied to the observed portion of the document's DOM tree.
注意:
The queue of mutations which have occurred, but not been delivered to the observer's callback is left empty after calling
takeRecords()
.
In this example, we demonstrate how to handle any undelivered
MutationRecord
s by calling
takeRecords()
just before disconnecting the observer.
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);
}
The code in lines 12–17 fetches any unprocessed mutation records, then invokes the callback with the records so that they can be processed. This is done immediately prior to calling
disconnect()
to stop observing the DOM.
| 规范 | 状态 | 注释 |
|---|---|---|
|
DOM
The definition of 'MutationObserver.takeRecords()' in that specification. |
实时标准 |
| 桌面 | 移动 | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
takeRecords
|
Chrome 18 | Edge 12 | Firefox 14 | IE 11 | Opera 15 | Safari 6 | WebView Android Yes | Chrome Android 18 | Firefox Android 14 | Opera Android 14 | Safari iOS 6 | Samsung Internet Android 1.0 |
完整支持
MutationObserver
observe()
takeRecords()