简体   繁体   English

有没有一种方法可以检测到何时将DOM节点从DOM中删除?

[英]Is there a way to detect when a DOM node is about to be removed from the DOM?

Is there a way to detect when a DOM node is about to be removed from the DOM? 有没有一种方法可以检测到何时将DOM节点从DOM中删除? ie. 即。 something like onBeforeNodeRemoved ? onBeforeNodeRemoved类的onBeforeNodeRemoved

I am currently using mutation observers and the node appears to have already been removed by this point. 我当前正在使用变异观察器,并且该节点似乎已被删除。

Edit: The use-case is that an AngularJS 1.x application is being removed from the DOM by AngularJS-unaware code, and I want to detect the DOM node removal and invoke rootScope.$destroy . 编辑:用例是未通过AngularJS识别的代码从DOM中删除了AngularJS 1.x应用程序,我想检测DOM节点删除并调用rootScope.$destroy

If I read your question correctly, it sounds like you're saying that by the time you get your mutation observers in place, the node you are trying to watch is already removed. 如果我正确阅读了您的问题,听起来您是在说,到位变异观察者到位时,您要监视的节点已被删除。 So you never manage to observe it. 因此,您永远无法观察到它。

If this is the case get your mutation observers into the page and setup before anything else at all. 如果是这种情况,请让您的变异观察者进入页面并进行设置,然后再进行其他设置。 It's the only way I can see that you would be able to observe "everything" that happens. 这是我看到您将能够观察到发生的“一切”的唯一方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM