class 的 MutationObserver(不适用于 id)

MutationObserver for class (not for id)

让 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);
}

您遇到了一些问题:

  • 迭代器:target[i] 不是您在执行代码后所期望的 (var foo = target[i].getAttribute("someAttribute")),因为当此行为 运行、i 时迭代已完成值为 target.length,因此 target[i] 不存在
  • 属性没有样式(foo.style.backgroundColor),需要引用目标元素
  • 您正在将整个集合传递给观察者 (observer.observe(target, config);) 您只需要一个目标元素

这是修复上面列出的错误并将循环代码外化到函数中以便于目标引用后的工作代码:

var target = document.querySelectorAll(".c");
for (var i = 0; i < target.length; i++) {
  create(target[i]);
}

function create(t) {
  // create an observer instance
  var observer = new MutationObserver(function(mutations) {
    mutations.forEach(function(mutation) {
      var foo = t.getAttribute("aaa")

      if (foo == "vvv")
        t.style.backgroundColor = "red";
    });
  });
  // configuration of the observer
  var config = {
    attributes: true
  };

  // pass in the target node, as well as the observer options
  observer.observe(t, config);
}

// let's change an attribute in a second
setTimeout(function(){
  target[2].setAttribute('aaa', 'vvv');
}, 1000);
.c {
  width: 50px;
  height: 50px;
  display: inline-block;
  border: 1px solid black
}
<div class="c"></div>
<div class="c"></div>
<div class="c"></div>
<div class="c"></div>


更新

这是一个经过最少编辑的示例:

  • var foo = target[i].getAttribute("someAttribute") 更改为 var foo = mutation.target.getAttribute("someAttribute") 而不是传入的目标元素

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 = mutation.target.getAttribute("someAttribute")

            if (foo == "someValue")
                mutation.target.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[i], config);
}

// let's change an attribute in a second
setTimeout(function(){
  target[2].setAttribute('someAttribute', 'someValue');
}, 1000);
.someClass {
  width: 50px;
  height: 50px;
  display: inline-block;
  border: 1px solid black
}
<div class="someClass"></div>
<div class="someClass"></div>
<div class="someClass"></div>
<div class="someClass"></div>