开发者

javascript compare two DOM trees

开发者 https://www.devze.com 2022-12-31 18:01 出处:网络
I want to compare the change of a DOM node after a user event is fired on it; but I don\'t know on which element a user would fire, so my idea is to (1) save the DOM tree before an event and (2) compa

I want to compare the change of a DOM node after a user event is fired on it; but I don't know on which element a user would fire, so my idea is to (1) save the DOM tree before an event and (2) compare the saved tree with the updated DOM tree when an event is fired.

My question are (1) is there any better way? and (2) if there is no other way, what would be the fast algorithm to com开发者_开发问答pare two DOM trees?


Since events bubble up, you could simply attach an event listener to the window object which catches all events, regardless of where they originated. You can examine the passed event object for event.target to see where it originated.


Depends on how complex a change you're expecting, but one possible "better" way would be to make use of the built in DOM events. Specifically, the DOMSubtreeModified event will fire whenever the part of the DOM tree you're watching gets changed.

0

精彩评论

暂无评论...
验证码 换一张
取 消