我需要利用这个 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>
Console:
> color red black
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)