javascript - MutationObserver not picking up on child nodes - Stack Overflow

Fiddle: I would like to have one MutationObserver that can detect all new nodes within itself. In the

Fiddle: /

I would like to have one MutationObserver that can detect all new nodes within itself. In the example, I have set {childList: true, subtree: true}, but div#nested does not appear in the MutationObserver (shown in the console).

How can I have an observer detects any depth of child nodes?

const domObserver = new MutationObserver((records) => {
  records.forEach((record) => {
    console.log(record)
  })
})

domObserver.observe(document.querySelector('#frame'), {childList: true, subtree: true})

// copy child nodes out of #template (as a string) and inject them into #frame for the observer to detect
document.querySelector('#frame').innerHTML = document.querySelector('#template').innerHTML
<div id="frame"></div>

<div id="template" style="display: none;">
   <div class="level-1">
    <div class="level-2">

      <div id="nested">
        I exist in the DOM but am not being seen by the MutationObserver
      </div>

    </div>
  </div>

  <div class="level-1">
    <div class="level-2">

    </div>
  </div>

  <div class="level-1">
    <div class="level-2">

    </div>
  </div>
</div>

Fiddle: https://jsfiddle/7gj26hqu/

I would like to have one MutationObserver that can detect all new nodes within itself. In the example, I have set {childList: true, subtree: true}, but div#nested does not appear in the MutationObserver (shown in the console).

How can I have an observer detects any depth of child nodes?

const domObserver = new MutationObserver((records) => {
  records.forEach((record) => {
    console.log(record)
  })
})

domObserver.observe(document.querySelector('#frame'), {childList: true, subtree: true})

// copy child nodes out of #template (as a string) and inject them into #frame for the observer to detect
document.querySelector('#frame').innerHTML = document.querySelector('#template').innerHTML
<div id="frame"></div>

<div id="template" style="display: none;">
   <div class="level-1">
    <div class="level-2">

      <div id="nested">
        I exist in the DOM but am not being seen by the MutationObserver
      </div>

    </div>
  </div>

  <div class="level-1">
    <div class="level-2">

    </div>
  </div>

  <div class="level-1">
    <div class="level-2">

    </div>
  </div>
</div>

Share Improve this question edited Apr 20, 2020 at 4:08 ggorlen 58k8 gold badges114 silver badges157 bronze badges asked Apr 20, 2020 at 4:01 somebodysomewheresomebodysomewhere 1,1561 gold badge13 silver badges26 bronze badges 1
  • It's working. It just takes a long time to load it all into the console. Do const domObserver = new MutationObserver(records=>{ records.forEach(record=>{ console.log(record.target.innerHTML); }); }); – StackSlave Commented Apr 20, 2020 at 4:27
Add a ment  | 

1 Answer 1

Reset to default 6

It looks like when a container that's being observed has its innerHTML set, the children of the container are emptied, and then new children get added fully intact. Using a synchronous observer (just so you can see what's happening), see how child nodes exist when the element is being appended:

// Look at results in browser console:

window.addEventListener('DOMNodeInserted', (e) => {
  console.log(e.path[0].childNodes.length);
});

document.querySelector('#frame').innerHTML = document.querySelector('#template').innerHTML
<div id="frame"></div>

<div id="template" style="display: none;">
   <div class="level-1">
    <div class="level-2">
    
      <div id="nested">
        I exist in the DOM but am not being seen by the MutationObserver
      </div>
      
    </div>
  </div>
  
  <div class="level-1">
    <div class="level-2">
    
    </div>
  </div>
  
  <div class="level-1">
    <div class="level-2">
    
    </div>
  </div>
</div>

The grandchild of the container being observed is attached to the child before the child gets attached to the container, so the subtree: true doesn't see the attachment action.

To detect all child nodes inserted this way, you'll have to recursively iterate through all elements in the MutationRecord manually, despite the subtree: true.

const recurse = (parent) => {
  
  console.log(parent);
  if (parent.childNodes) {
    [...parent.childNodes].forEach(recurse);
  }
};
const domObserver = new MutationObserver((records) => {
  for (const record of records) {
    for (const node of record.addedNodes) {
      recurse(node);
    }
  }
})

domObserver.observe(document.querySelector('#frame'), {childList: true, subtree: true})

// copy child nodes out of #template (as a string) and inject them into #frame for the observer to detect
document.querySelector('#frame').innerHTML = document.querySelector('#template').innerHTML
<div id="frame"></div>

<div id="template" style="display: none;">
   <div class="level-1">
    <div class="level-2">

      <div id="nested">
        I exist in the DOM but am not being seen by the MutationObserver
      </div>

    </div>
  </div>

  <div class="level-1">
    <div class="level-2">

    </div>
  </div>

  <div class="level-1">
    <div class="level-2">

    </div>
  </div>
</div>

Result:

If you want to use a TreeWalker instead:

const frame = document.querySelector('#frame');
const domObserver = new MutationObserver(() => {
  // If the container's innerHTML was assigned to, iterate over all descendants:
  const treeWalker = document.createTreeWalker(frame);
  const nodes = [];
  let currentNode = treeWalker.currentNode;
  while (currentNode) {
    nodes.push(currentNode);
    currentNode = treeWalker.nextNode();
  }
  console.log(nodes);
});

domObserver.observe(frame, {
  childList: true,
  subtree: true
})

// copy child nodes out of #template (as a string) and inject them into #frame for the observer to detect
document.querySelector('#frame').innerHTML = document.querySelector('#template').innerHTML
<div id="frame"></div>

<div id="template" style="display: none;">
  <div class="level-1">
    <div class="level-2">

      <div id="nested">
        I exist in the DOM but am not being seen by the MutationObserver
      </div>

    </div>
  </div>

  <div class="level-1">
    <div class="level-2">

    </div>
  </div>

  <div class="level-1">
    <div class="level-2">

    </div>
  </div>
</div>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745171097a4614929.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信