DOM 突变事件替换 [英] DOM mutation events replacement

查看:23
本文介绍了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 ?

推荐答案

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

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:

  • 为要监控的属性定义 getter 和 setter
  • 覆盖诸如 document.createAttributeattributes.setNamedItem、...
  • 之类的方法
  • 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:

这篇关于DOM 突变事件替换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆