是否有替代 DOMAttrModified 的方法可以在 webkit 中使用 [英] is there an alternative to DOMAttrModified that will work in webkit

查看:35
本文介绍了是否有替代 DOMAttrModified 的方法可以在 webkit 中使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要利用这个 DOM 事件.IE 有 onpropertychange,它也做我需要它做的事情.但是,Webkit 似乎不支持此事件.我可以使用其他替代方法吗?

I need to leverage this DOM event. IE has onpropertychange, which does what I need it to do also. Webkit doesn't seem to support this event, however. Is there an alternative I could use?

推荐答案

虽然 Chrome 不发送 DOMAttrModified 事件,但自 2011 年起支持更轻量级的突变观察者,这些也适用于属性更改.

Although Chrome does not dispatch DOMAttrModified events, the more lightweighted mutation observers are supported since 2011 and these work for attribute changes, too.

以下是文档正文的示例:

Here is an example for the document body:

var element = document.body, bubbles = false;

var observer = new WebKitMutationObserver(function (mutations) {
  mutations.forEach(attrModified);
});
observer.observe(element, { attributes: true, subtree: bubbles });

function attrModified(mutation) {
  var name = mutation.attributeName,
    newValue = mutation.target.getAttribute(name),
    oldValue = mutation.oldValue;

  console.log(name, newValue, oldValue);
}

对于简单的属性更改,console.log 语句将打印:

For a simple attribute change, the console.log statement would print:

<body color="black">
<script type="text/html">
document.body.setAttribute("color", "red");
</script>
</body>

控制台:

<代码>>颜色红黑

这篇关于是否有替代 DOMAttrModified 的方法可以在 webkit 中使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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