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

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

问题描述

我需要利用这个DOM事件。 IE有一个专业的交换,这也是我需要做的。但是,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>

控制台:

>颜色红黑色

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

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