使用 MutationObserver 时,DOM 中似乎不存在 ChildNode
我正在尝试使用 MutationObserver 来观察 DOM 中的变化,但似乎我无法访问“大”子节点。我已经使用 subtree 和 childList 值配置了观察者。据我了解,使用 MutationObserver 不可能获取添加的 childList 或更改的整个 DOM 树,它所做的只是观察更改。相反,您应该使用 getElementById。
我尝试在观察到更改后使用 getElementById 在 DOM 中查找相关的“父”节点,然后抓取所有子节点。尽管我仍然没有得到子节点的命中。
我假设“父”节点首先插入到 DOM 上,然后将子节点插入到“父”节点上,尽管由于某种原因这些事件不会在观察者中触发。
我怀疑我可能需要在观察变化时更新 MutationObserver 的目标,然后不断使用 getElementById 并爬取这些节点。
知道为什么这些子节点不可观察,和/或如何解决这个问题吗?
此致。
MutationObserver 的代码
function createObserver() {
const documentBody = document.body;
// callback function to execute when mutations are observed
const observer = new MutationObserver(mutationRecords => {
let addedNodes = []
for (const mut of mutationRecords) {
let arr = Array.prototype.slice.call(mut.addedNodes)
arr = arr.filter(node => popupTagNames.includes(node.tagName)); // Keep only selected tags
if (arr.length == 0) return; // don't keep empty
addedNodes = addedNodes.concat(arr)
let el = document.getElementById(addedNodes[0].id);
// Crawler
inspectNode(el)
}
})
const config = { attributes: true, childList: true, subtree: true, characterData: true }
observer.observe(documentBody, config)
}
I am trying to use MutationObserver to observe changes in the DOM but it seems like I can't access the "grand"-children nodes. I have configured the observer with subtree and childList values. As I understand, it is not possible to get the entire DOM-tree of added childLists or changes with the MutationObserver, all it does is observe the changes. Instead you are supposed to use getElementById.
I've tried using getElementById to find the relevant "parent"-node in the DOM after the change is observed, and then crawl all the childNodes. Although I still get no hits on the childNodes.
I assume the "parent"-node is inserted onto the DOM first, and the childNodes are then inserted on to the "parent"-node after the fact, though these events are not triggered in the observer for some reason.
I suspect I might need to update the target of the MutationObserver as I observe the changes, and then continuously use getElementById and crawl these nodes.
Any idea on why these childNodes are not observable, and/or how to approach the solution to this?
Best regards.
Code to MutationObserver
function createObserver() {
const documentBody = document.body;
// callback function to execute when mutations are observed
const observer = new MutationObserver(mutationRecords => {
let addedNodes = []
for (const mut of mutationRecords) {
let arr = Array.prototype.slice.call(mut.addedNodes)
arr = arr.filter(node => popupTagNames.includes(node.tagName)); // Keep only selected tags
if (arr.length == 0) return; // don't keep empty
addedNodes = addedNodes.concat(arr)
let el = document.getElementById(addedNodes[0].id);
// Crawler
inspectNode(el)
}
})
const config = { attributes: true, childList: true, subtree: true, characterData: true }
observer.observe(documentBody, config)
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
让我们通过记录属于 #qc-cmp2-container 元素的添加节点来进行调查:
我们将看到几个单独的调用:
最节省资源的解决方案是使用超快速 getElementById 检查等待父级,然后切换到观察父级:
Let's investigate by logging the added nodes that belong to #qc-cmp2-container element:
We'll see several separate calls:
The most resource-effective solution is to wait for the parent using the super fast getElementById check and then switch to observing the parent: