【发布时间】:2016-05-07 22:56:00
【问题描述】:
让 MutationObserver 为#someID 工作不是问题,但是如何让它为.someClass 工作?
目前我正在使用以下内容:
// this example doensn't work,
// as well as many another attempts
var target = document.querySelectorAll(".someClass");
for (var i = 0; i < target.length; i++) {
// create an observer instance
var observer = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
var foo = target[i].getAttribute("someAttribute")
if (foo == "someValue")
foo.style.backgroundColor = "red";
});
});
// configuration of the observer
var config = { attributes: true };
// pass in the target node, as well as the observer options
observer.observe(target, config);
}
【问题讨论】:
标签: javascript mutation-observers