弃用
This feature is no longer recommended. Though some browsers might still support it, it may have already been removed from the relevant web standards, may be in the process of being dropped, or may only be kept for compatibility purposes. Avoid using it, and update existing code if possible; see the
兼容性表格
at the bottom of this page to guide your decision. Be aware that this feature may cease to work at any time.
MutationEvent
interface provides event properties that are specific to modifications to the Document Object Model (DOM) hierarchy and nodes.
注意: Mutation Events (W3C DOM Level 3 Events) have been deprecated in favor of Mutation Observers (W3C DOM4).
The mutation events have been marked as deprecated in the DOM Events specification , as the API's design is flawed (see details in the "DOM Mutation Events Replacement: The Story So Far / Existing Points of Consensus" post to public-webapps ) .
Mutation Observers are the proposed replacement for mutation events in DOM4. They are expected to be included in Firefox 14 and Chrome 18 .
The practical reasons to avoid the mutation events are performance issues and cross-browser support .
Adding DOM mutation listeners to a document profoundly degrades the performance of further DOM modifications to that document (making them 1.5 - 7 times slower!). Moreover, removing the listeners does not reverse the damage.
The performance effect is limited to the documents that have the mutation event listeners .
These events are not implemented consistently across different browsers, for example:
Dottoro documents browser support for mutation events .
The following is a list of all mutation events, as defined in DOM Level 3 Events specification :
DOMAttrModified
DOMAttributeNameChanged
DOMCharacterDataModified
DOMElementNameChanged
DOMNodeInserted
DOMNodeInsertedIntoDocument
DOMNodeRemoved
DOMNodeRemovedFromDocument
DOMSubtreeModified
You can register a listener for mutation events using
EventTarget.addEventListener()
如下:
element.addEventListener("DOMNodeInserted", function (event) {// ...}, false);
The event object is passed to the listener in a
MutationEvent
(见
its definition in the specification
) for most events, and
MutationNameEvent
for
DOMAttributeNameChanged
and
DOMElementNameChanged
.
No compatibility data found. Please contribute data for "api.MutationEvent" (depth: 1) to the MDN 兼容性数据存储库 .