I know this is an old question, that already has an answer, but since things have changed, I thought I'd add an updated answer for people landing on this page looking for an answer.
The DOM Mutation Events have been deprecated. According to MDN (regarding DOM Mutation Events
):
Deprecated
This feature has been removed from the Web. Though some browsers may still support it, it is in the process of being dropped. Do not use it in old or new projects. Pages or Web apps using it may break at any time.
One should use the new MutationObserver API, which is also more efficient.
(The mutation-summary library now provides a useful inteface to this new API.)
Example usage:
// Create an observer instance.
var observer = new MutationObserver(function (mutations) {
mutations.forEach(function (mutation) {
console.log(mutation.type);
});
});
// Config info for the observer.
var config = {
childList: true,
subtree: true
};
// Observe the body (and its descendants) for `childList` changes.
observer.observe(document.body, config);
...
// Stop the observer, when it is not required any more.
observer.disconnect();
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…