javascript - 类的 MutationObserver(不适用于 id)

标签 javascript mutation-observers

让 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>

关于javascript - 类的 MutationObserver(不适用于 id),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35097520/

相关文章:

javascript - 添加没有html的文本悬停图像

javascript - 监视具有特定属性的元素

javascript - 检测节点何时被删除(或从 DOM 中删除,因为父节点是)

javascript - 带距离的气泡标签云

javascript - SlickGrid 并从单元格复制文本

javascript - 使用 jquery 在向下一些像素后显示按钮

javascript - Javascript 中函数(全局)的含义

javascript - 绕过现有的 MutationObserver 以使用 Tampermonkey 插入 DOM 元素

javascript - DOM MutationObservers:如何支持DOM3突变事件的这一重要用途?

javascript - 如何使用 jQuery 检测 HTML 元素是否消失?