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.
实时标准

浏览器兼容性

The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request. 更新 GitHub 上的兼容性数据
桌面 移动
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
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

图例

完整支持

完整支持

元数据

  • 最后修改:
  1. MutationObserver
  2. 构造函数
    1. MutationObserver()
  3. 方法
    1. observe()
    2. takeRecords()