MutationObserver is a Web API built-in object that is an observer object that listens for changes to a specified DOM node. Exists by default and is compatible with almost all browsers above IE11.
create
Callback is created as an instance, with a single argument that specifies the callback function when the object triggers the change event:
const callback = records= > { console.log(records) }
const observer = new MutationObserver(callback)
// Observe all changes except for features
observer.observe(ele, {
childList: true.// Observe the direct child node
subtree: true.// and lower descendant nodes
characterDataOldValue: true // Pass old data to the callback
});
/** Records example [{addedNodes: NodeList [] attributeName: null attributeNamespace: null nextSibling: null oldValue: "bold" previousSibling: null removedNodes: NodeList [] target: text type: "characterData" }] **Copy the code
methods
So how do you specify how to listen for DOM elements? The observer instance has three methods to implement related operations:
observe(target, MutationObserverInit)
: Sets the listening object,target
Is a goalNode
Object, and the second argument is aMutationObserverInit
Listening parameter objectdisconnect()
: Stop listening. Invoke observe againtakeRecords()
: Deletes the listener notification queue and returns the current queue as an arrayMutationRecord
Object to retrieve the remaining events before stopping listening
Monitoring parametersMutationObserverInit
:
- Monitoring scope
childList
Boolean, whether to monitor the target nodeAnd its child nodes
Change to defaultfalse
subtree
Boolean, whether to listen to the target nodeAnd all descendant nodes
Change to defaultfalse
- Monitor properties
attributes
Boolean, whether to listen for attribute value changes, defaultfalse
attrbuteFilter
, string[], specifies the range of listening propertiesattributeOldValue
, Boolean, whether to return the old attribute value
- Listen to the text
characterData
, Boolean, whether to listen for #textThe text content
The change ofcharacterDataOldValue
Boolean, whether to return old #textThe text content
The response objectMutationRecord
:
Callback (Records) is triggered with an array of Records in the MutationRecord object to tell the developer what the DOM change is:
type
Change the type- ‘attributes’ properties
- ‘characterData’ text
- ‘childList’ adds/deletes child nodes
target
Change occurring elementaddedNodes/removedNodes
Nodes added or deletedpreviousSibling/nextSibling
The upper/next node of the added/deleted nodeattributeName/attributeNamespace
Name/namespace of the property being changed (for XML)oldValue
Previous value (property/text), if setattributeOldValue
/characterDataOldValue
Will appear
Here’s a more complete example:
<div contentEditable id="elem">Click and <b>edit</b>, please</div>
<script>
let observer = new MutationObserver(mutationRecords= > {
console.log(mutationRecords); // console.log(the changes)
});
// Observe all changes except for features
observer.observe(elem, {
childList: true.// Observe the direct child node
subtree: true.// and lower descendant nodes
characterDataOldValue: true // Pass old data to the callback
});
</script>
Copy the code
The above. Original is not easy, would like to turn first inform, welcome to add my exchange: Cassius