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.
代码片段如下:
1 2 3 4 5 6 7 8 9 10 11 12
var observeDOMChanges = (function () { // ... var bindMutationEvents = function (eventName) { document.body.addEventListener(eventName, function (e) { changedHandler(e.target); }); }; returnfunction () { bindMutationEvents('DOMSubtreeModified'); bindMutationEvents('DOMNodeInserted'); }; })();
// test var el = document.createElement('a'); // works in IE and webkit via `DOMNodeInserted` document.body.appendChild(el); setTimeout(function() { // <a> -> <a href="xxx"> // works in IE and webkit via `DOMSubtreeModified` el.href = 'path/to/somewhere'; setTimeout(function() { // <a href="a"> -> <a href="b"> // works in IE via `DOMSubtreeModified` // NOT works in webkit el.href = 'path/to/somewhere/else'; }, 1000); }, 1000);