DOM 突变事件替换
由于 DOM 突变已被 w3c 标记为已弃用(请参阅 http: //www.w3.org/TR/DOM-Level-3-Events/#events-mutationevents),是否有一种(快速)替代方法来检测 DOM 中的属性修改?
Since DOM mutation is marked as deprecated by the w3c (see http://www.w3.org/TR/DOM-Level-3-Events/#events-mutationevents), is there an (fast) alternative way to detect attribute modification in the DOM ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
突变事件被弃用的原因是巨大的性能问题。
替代品是Mutation Observers,请查看http://updates.html5rocks.com/2012/02/Detect-DOM-changes-with-Mutation-Observers 和 https://developer.mozilla.org/en/DOM/DOM_Mutation_Observers
有关突变的信息作为 MutationRecords 的有序序列传递给观察者,表示观察到的变化序列发生
示例用法:
Chrome 18、Firefox 和 Webkit nightly 版本支持此功能。 Firefox 14 也将支持此功能。
The reason that mutation events was deprecated was because of huge performance issues.
The replacement is Mutation Observers, look at http://updates.html5rocks.com/2012/02/Detect-DOM-changes-with-Mutation-Observers and https://developer.mozilla.org/en/DOM/DOM_Mutation_Observers
Information about mutations is delivered to observers as an ordered sequence of MutationRecords, representing an observed sequence of changes that have occurred
Sample usage:
This is supported in Chrome 18 and Firefox and Webkit nightly builds. Firefox 14 will also be supporting this feature.
与 CSS 动画结合使用的
animationStart
是已弃用的 DOM* 事件的一个很好的替代品。 David Walsh 撰写了有关该方法的文章。首先,设置关键帧并将其应用到您想要监听的元素(不要忘记供应商前缀!):
接下来,添加监听器:
Ta-da!这是David 的演示。它在 Chrome 扩展上非常适合我,将 Facebook 图片添加到 Google Voice(请参阅 content.css 并注入。 js)。
A great replacement for the deprecated DOM* events is
animationStart
in conjunction with CSS Animations. David Walsh writes about the method.First, set up the keyframes and apply it to the elements you'd like to listen for (don't forget the vendor prefixes!):
Next, add the listener:
Ta-da! Here is David's demo. It works great for me on a Chrome extension that adds Facebook pictures to Google Voice (see content.css and injected.js).
一年后,出现了新的、闪亮的
突变观察者
来自 DOM Level 4(点击那里的链接,它们解释了很多!)。Mutation Event
触发了一千次,而MutationObserver
仅触发一次,所有修改都包含且可访问。适用于(截至 2017/03):
window.WebKitMutationObserver
)window.WebKitMutationObserver )
A year later, there are the new and shiny
Mutation Observers
from DOM Level 4 (follow the links there, they explain a lot!). Where aMutation Event
fired a thousand times,MutationObserver
fires only once with all the modifications contained and accessible.Works for (as of 2017/03):
window.WebKitMutationObserver
)window.WebKitMutationObserver
)据我所知,目前还没有其他选择,因此您只能使用仅在 Firefox 和 Opera 中受支持的
DOMAttrModified
。在 IE 中,您有onpropertychanged
事件,但在 Chrome/Safari 中无法获得类似的功能。根据您想要完成的任务和目标浏览器,您可以执行许多操作:document.createAttribute
、< code>attributes.setNamedItem, ...我自己一直在研究跨浏览器解决方案,但没有取得太大成功。您应该远离突变事件,因为它们不是跨浏览器的并且非常慢。
它们被弃用是有充分理由的。如果您想了解更多信息,请阅读:
As far as I know there is no alternative (yet) so you are stuck with
DOMAttrModified
which is only supported in Firefox and Opera. In IE you have theonpropertychanged
event but there is no way to get similar functionality in Chrome/Safari. There are a number of things you could do depending on what you are trying to accomplish and the browsers you are targetting:document.createAttribute
,attributes.setNamedItem
, ...I've been working on a cross-browser solution myself but without much success. You should stay away from mutation events all together since they are not cross-browser and very slow.
There are good reasons why they are deprecated. If you want to learn more read this: