MutationRecord

每个 MutationRecord 都代表一个独立的 DOM 变化,在每次随 DOM 变化调用 MutationObserver 的回调函数时,一个相应的 MutationRecord 会被作为参数,传递给回调函数。

属性

属性 类型 描述
MutationRecord.type (en-US) String 如果是属性变化,则返回 "attributes"
如果是 characterData 节点变化,则返回 "characterData"
如果是子节点树 childList 变化,则返回 "childList"
MutationRecord.target (en-US) Node 根据 MutationRecord.type (en-US),返回变化所影响的节点。
对于属性 attributes 变化,返回属性变化的节点。
对于 characterData 变化,返回 characterData 节点。
对于子节点树 childList 变化,返回子节点变化的节点。
MutationRecord.addedNodes (en-US) NodeList 返回被添加的节点。
如果没有节点被添加,则该属性将是一个空的 NodeList
MutationRecord.removedNodes (en-US) NodeList 返回被移除的节点。
如果没有节点被移除,则该属性将是一个空的 NodeList
MutationRecord.previousSibling (en-US) Node 返回被添加或移除的节点之前的兄弟节点,或者 null
MutationRecord.nextSibling (en-US) Node 返回被添加或移除的节点之后的兄弟节点,或者 null
MutationRecord.attributeName (en-US) String 返回被修改的属性的属性名,或者 null
MutationRecord.attributeNamespace (en-US) String 返回被修改属性的命名空间,或者 null
MutationRecord.oldValue (en-US) String

返回值取决于 MutationRecord.type (en-US)
对于属性 attributes 变化,返回变化之前的属性值。
对于 characterData 变化,返回变化之前的数据。
对于子节点树 childList 变化,返回 null

注意,如果要让这个属性起作用,在相应的 MutationObserverInit 参数的 MutationObserver observe 方法中,attributeOldValue 或者 characterDataOldValue 必须设置为 true

规范

Specification
DOM Standard
# interface-mutationrecord

浏览器兼容性

BCD tables only load in the browser