DOM 突变事件替换

发布于 2024-10-25 22:51:36 字数 221 浏览 4 评论 0原文

由于 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

白云悠悠 2024-11-01 22:51:36

突变事件被弃用的原因是巨大的性能问题。

替代品是Mutation Observers,请查看http://updates.html5rocks.com/2012/02/Detect-DOM-changes-with-Mutation-Observershttps://developer.mozilla.org/en/DOM/DOM_Mutation_Observers

有关突变的信息作为 MutationRecords 的有序序列传递给观察者,表示观察到的变化序列发生

示例用法:

    var observer = new MutationObserver(function(mutationRecords) {
    // Handle mutations
     });

    observer.observe(myNode,
     {  // options:
     subtree: true,  // observe the subtree rooted at myNode
     childList: true,  // include information childNode insertion/removals
     attribute: true  // include information about changes to attributes within the subtree
    });

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:

    var observer = new MutationObserver(function(mutationRecords) {
    // Handle mutations
     });

    observer.observe(myNode,
     {  // options:
     subtree: true,  // observe the subtree rooted at myNode
     childList: true,  // include information childNode insertion/removals
     attribute: true  // include information about changes to attributes within the subtree
    });

This is supported in Chrome 18 and Firefox and Webkit nightly builds. Firefox 14 will also be supporting this feature.

谁的新欢旧爱 2024-11-01 22:51:36

与 CSS 动画结合使用的 animationStart 是已弃用的 DOM* 事件的一个很好的替代品。 David Walsh 撰写了有关该方法的文章。

首先,设置关键帧并将其应用到您想要监听的元素(不要忘记供应商前缀!):

@keyframes nodeInserted {  
  from { clip: rect(1px, auto, auto, auto); }
  to { clip: rect(0px, auto, auto, auto); }  
}

#parentElement > li {
  animation-duration: 0.001s;
  animation-name: nodeInserted;
}

接下来,添加监听器:

var insertListener = function(event){
  if (event.animationName == "nodeInserted") {
    // This is the debug for knowing our listener worked!
    // event.target is the new node!
    console.warn("Another node has been inserted! ", event, event.target);
  }
}
document.addEventListener("animationstart", insertListener, false); // standard + firefox
document.addEventListener("MSAnimationStart", insertListener, false); // IE
document.addEventListener("webkitAnimationStart", insertListener, false); // Chrome + Safari

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!):

@keyframes nodeInserted {  
  from { clip: rect(1px, auto, auto, auto); }
  to { clip: rect(0px, auto, auto, auto); }  
}

#parentElement > li {
  animation-duration: 0.001s;
  animation-name: nodeInserted;
}

Next, add the listener:

var insertListener = function(event){
  if (event.animationName == "nodeInserted") {
    // This is the debug for knowing our listener worked!
    // event.target is the new node!
    console.warn("Another node has been inserted! ", event, event.target);
  }
}
document.addEventListener("animationstart", insertListener, false); // standard + firefox
document.addEventListener("MSAnimationStart", insertListener, false); // IE
document.addEventListener("webkitAnimationStart", insertListener, false); // Chrome + Safari

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).

多情癖 2024-11-01 22:51:36

一年后,出现了新的、闪亮的突变观察者 来自 DOM Level 4(点击那里的链接,它们解释了很多!)。 Mutation Event 触发了一千次,而 MutationObserver 仅触发一次,所有修改都包含且可访问。

适用于(截至 2017/03):

  • Firefox 14+
  • IE 11
  • Edge
  • Opera 15+
  • Chrome 26+(18 至 25 前缀,window.WebKitMutationObserver
  • Safari 6.0(前缀,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 a Mutation Event fired a thousand times, MutationObserver fires only once with all the modifications contained and accessible.

Works for (as of 2017/03):

  • Firefox 14+
  • IE 11
  • Edge
  • Opera 15+
  • Chrome 26+ (18 till 25 prefixed, window.WebKitMutationObserver)
  • Safari 6.0 (prefixed, window.WebKitMutationObserver)
2024-11-01 22:51:36

据我所知,目前还没有其他选择,因此您只能使用仅在 Firefox 和 Opera 中受支持的 DOMAttrModified。在 IE 中,您有 onpropertychanged 事件,但在 Chrome/Safari 中无法获得类似的功能。根据您想要完成的任务和目标浏览器,您可以执行许多操作:

  • 为您想要监视的属性定义 getter 和 setter
  • 覆盖方法,例如 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 the onpropertychanged 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:

  • define getters and setters to the attributes you want to monitor
  • override methods like 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:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文