为什么 DOMSubtreeModified 事件在 DOM 级别 3 中被弃用?

发布于 2024-11-19 11:24:53 字数 133 浏览 3 评论 0原文

为什么 DOMSubtreeModified 事件已弃用以及我们应该使用什么反而?

Why is the DOMSubtreeModified event deprecated and what are we supposed to use instead?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

朦胧时间 2024-11-26 11:24:53

如果您向下滚动一点,您会看到:

警告! MutationEvent 接口是在 DOM Level 2 中引入的
事件,但尚未完全且可互操作地实现
跨用户代理。此外,也有批评称
按照设计,接口引入了性能和实现
挑战。新规范正在开发中,目的是
解决突变事件解决的用例,但更多的是
表现方式。因此,本规范描述了突变事件
以供参考和遗留行为的完整性,但不赞成
同时使用 MutationEvent 接口和 MutationNameEvent
界面。

替换 API 是突变观察者,它是完全指定的DOM Living Standard 中的内容,取代了所有 DOM X 级的愚蠢行为。

If you scroll down a bit, you see:

Warning! The MutationEvent interface was introduced in DOM Level 2
Events, but has not yet been completely and interoperably implemented
across user agents. In addition, there have been critiques that the
interface, as designed, introduces a performance and implementation
challenge. A new specification is under development with the aim of
addressing the use cases that mutation events solves, but in more
performant manner. Thus, this specification describes mutation events
for reference and completeness of legacy behavior, but deprecates the
use of both the MutationEvent interface and the MutationNameEvent
interface.

The replacement API is mutation observers, which are fully specified in the DOM Living Standard that supercedes all of the DOM level X silliness.

·深蓝 2024-11-26 11:24:53

我认为替代者将是突变观察者:https://developer.mozilla.org/en-US /docs/Web/API/MutationObserver

var whatToObserve = {childList: true, attributes: true, subtree: true, attributeOldValue: true, attributeFilter: ['class', 'style']};
var mutationObserver = new MutationObserver(function(mutationRecords) {
  $.each(mutationRecords, function(index, mutationRecord) {
    if (mutationRecord.type === 'childList') {
      if (mutationRecord.addedNodes.length > 0) {
        //DOM node added, do something
      }
      else if (mutationRecord.removedNodes.length > 0) {
        //DOM node removed, do something
      }
    }
    else if (mutationRecord.type === 'attributes') {
      if (mutationRecord.attributeName === 'class') {
        //class changed, do something
      }
    }
  });
});
mutationObserver.observe(document.body, whatToObserve);

I think the replacement will be mutation observers: https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver

var whatToObserve = {childList: true, attributes: true, subtree: true, attributeOldValue: true, attributeFilter: ['class', 'style']};
var mutationObserver = new MutationObserver(function(mutationRecords) {
  $.each(mutationRecords, function(index, mutationRecord) {
    if (mutationRecord.type === 'childList') {
      if (mutationRecord.addedNodes.length > 0) {
        //DOM node added, do something
      }
      else if (mutationRecord.removedNodes.length > 0) {
        //DOM node removed, do something
      }
    }
    else if (mutationRecord.type === 'attributes') {
      if (mutationRecord.attributeName === 'class') {
        //class changed, do something
      }
    }
  });
});
mutationObserver.observe(document.body, whatToObserve);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文