在 designMode 中查找 Iframe 内容何时更改,可以通过监听 Iframe 的 DOMSubtreeModified
事件来实现。DOMSubtreeModified
事件会在 Iframe 的 DOM 结构发生变化时触发。
以下是一个示例代码:
const iframe = document.querySelector('iframe');
iframe.addEventListener('DOMSubtreeModified', () => {
console.log('Iframe 内容已更改');
});
需要注意的是,DOMSubtreeModified
事件已经被标记为废弃,因此在新的项目中不建议使用。可以考虑使用 MutationObserver
来替代。
以下是使用 MutationObserver
的示例代码:
const iframe = document.querySelector('iframe');
const observer = new MutationObserver(() => {
console.log('Iframe 内容已更改');
});
observer.observe(iframe.contentDocument, {
childList: true,
subtree: true,
});
在这个示例中,我们创建了一个 MutationObserver
实例,并将 Iframe 的 contentDocument
作为观察对象。当 Iframe 的 DOM 结构发生变化时,MutationObserver
会触发回调函数,输出 "Iframe 内容已更改"。
领取专属 10元无门槛券
手把手带您无忧上云