我有一个 iframe 正在呈现来自另一个域的内容。显然我无法contentDocument
从其他域访问(返回null
)。但是当用户对呈现的 iframe 执行操作时,它会重定向回我的域。因此,那时我可以阅读该文档。基本上一旦 contentDocument 不是null
,我想执行一个动作。
目前我只是想在 iframe 的孩子(在这种情况下它contentDocument
发生变异......
window.onload = () => {
setTimeout(initializeObserver, 2000)
}
const initializeObserver = () => {
const iframe = document.getElementById('my_iframe')
observer.observe(
iframe,
{
attributes: true,
childList: true,
subtree: true
}
)
}
const observer = new MutationObserver(mutations => {
mutations.forEach(mutation => {
if(mutation.addedNodes.length) {
alert("SOMETHING HAPPENED!")
}
})
})
但是即使添加了子节点并且 iframe 中的元素发生了变化,警报也不会触发。我读过很多例子,但也许我仍然想念它是如何工作的。有人可以帮助解释为什么这个观察者没有触发突变吗?
提前致谢