我目前有代码:
$('.example').bind('DOMNodeInserted DOMNodeRemoved', function(event) {
....
});
效果很好,但效率不高,此后已被弃用。有什么更好的方法来做到这一点?
我一直在研究 MutationObserver 但这段代码有效吗?
它给出了错误“mutation.addedNodes
不是函数”,我也需要removedNodes
我意识到。
var observer = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
mutation.addedNodes.forEach(function(node) {
if (node.className == 'example') {
....
}
});
});
});
observer.observe(document, {
childList: true,
subtree: true,
attributes: false,
characterData: false,
});