How do you do something if the DOM is changed?

随声附和 提交于 2019-12-13 16:24:52

问题


How can I detect when the document changes?

I know I could do something like this:

var lastHTML = document.body.innerHTML;
setInterval(function() { if(document.body.innerHTML != lastHTML) { DOMupdated() } }, 500);

But is there an event handler I can put in place, like onchange?


回答1:


There's the old DOM Mutation Events, and there's the new DOM Mutation Observers.


For a quick overview of them both (and why the newer [more complex] API is better), check out this post:

Detect DOM Changes with Mutation Observers.


P.S. Don't miss the comments there...



来源:https://stackoverflow.com/questions/12102391/how-do-you-do-something-if-the-dom-is-changed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!