我需要利用这个 DOM 事件。 IE 有 onpropertychange,它也可以做我需要它做的事情。然而,Webkit 似乎不支持这个事件。有没有我可以使用的替代方案?
最佳答案
虽然 Chrome 不发送 DOMAttrModified
事件,自 2011 年以来支持更轻量级的突变观察者,这些也适用于属性更改。
以下是文档正文的示例:
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
声明将打印:<body color="black">
<script type="text/html">
document.body.setAttribute("color", "red");
</script>
</body>
安慰:
> color red black
关于dom - 是否有可以在 webkit 中使用的 DOMAttrModified 的替代方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1882224/