1 varmutationobserver = window. Mutationobserver | | Window. Webkitmutationobserver | | Window. Mozmutationobserver;//Browser compatible2 varConfig = {attributes:true, Childlist:true}//Configuration Objects3$ ("DOM element to listen for"). each (function(){4 var_this = $ ( This);5 varObserver =NewMutationobserver (function(mutations) {//Constructor Callback6Mutations.foreach (function(record) {7 if(Record.type = = "Attributes") {//Listening Properties8 //Do any code9 }Ten if(Record.type = = ' Childlist ') {//The monitoring structure has changed . One //Do any code A } - }); - }); theObserver.observe (_this[0], config); -});
Configuration Object Config Property
- Childlist: Changes in child elements
- Attributes: Changes in attributes
- Characterdata: Changes in node content or node text
- Subtree: Changes to all subordinate nodes (including child nodes and child nodes)
- Attributefilter: Monitoring development properties [Attrname]
Note: Subtree can not be used alone, and other properties should be combined;
Stop monitoring observation
Observer.disconnect ();
Clear History Listening record
Observer.takerecord
Record returns the properties of an object
- Type: Types of observed changes (attribute, Characterdata, or childlist).
- Target: The DOM object that has changed.
- Addednodes: New DOM object.
- Removenodes: The deleted DOM object.
- PreviousSibling: The previous sibling of the DOM object, or null if none.
- NextSibling: The next sibling of the DOM object, if not, returns NULL.
- AttributeName: The property in which the change occurred. If Attributefilter is set, only pre-specified properties are returned.
- OldValue: The value before the change. This property is only valid for attribute and characterdata changes, and returns null if a childlist change occurs.
Mutationobserver Monitoring DOM element structure change and attribute change instances