【发布时间】:2020-11-09 04:18:53
【问题描述】:
我正在尝试使用 MutationObserver 检查页面上的每个突变。
有没有一种简单的方法来解决以下问题,这样我就不必将它作为内联脚本包含在正文中(即包含在 <head> 中)?
问题在于我需要bodyList 变量才能开始监控添加到页面的项目。
显然我不能使用 onLoad 事件或任何东西,因为那时突变已经全部发生(除非在我不知道的事实之后有办法访问它们?)。
或者有没有办法将突变观察者附加到文档本身而不是元素?
我确信答案很简单,但我找不到任何涵盖此内容的文档。
<body>
<script>
//I want to move this inline script into the <head>
var bodyList = document.querySelector("body"),
observer = new MutationObserver(function (mutations) {
mutations.forEach(function (mutation) {
console.log("MUTATION", mutation)
});
});
observer.observe(bodyList, {childList: true, subtree: true});
</script>
....all the HTML elements that I want to monitor
【问题讨论】:
标签: javascript mutation-observers