iframe - 使用 Svelte 访问 iframe 内容
问题描述
我正在尝试访问 iframe 中的 head 元素以更改样式。但是我能想到的一切都行不通。
这是我当前的代码:
<script>
let frame;
onMount(() => {
frame.addEventListener('load', onLoad());
})
function onLoad() {
let head = frame.contentDocument.head || frame.contentWindow.document.head;
console.log(head);
}
</script>
<iframe bind:this={frame} src="src_here" title="preview" />
这将成功记录 iframe 但innerHTML
为空白。
解决方案
我认为您不能很好地访问 head 元素。
尝试这样的事情:
function onLoad() {
const head = frame.contentDocument.querySelector('head');
console.log(head);
}
推荐阅读
- sql - 将 48 小时添加到日期以创建新日期,将 48 小时添加到新日期并重复直到到达结束日期
- python - 增加 Pyarrow FlightStreamReader 的块大小
- assembly - 如何在bios汇编中打印一个数字
- omnet++ - 从 Omnetpp 中的不同运行收集结果时出现问题
- c++ - cmake find_package,find_library可以定位glfw,但是TARGET glfw为false
- javascript - 像google keep这样的项目,但是当我单击单个删除时,它会删除所有内容
- javascript - Bootstrap 4 崩溃无法正常工作
- gpu - 为什么 Spacy 3 NER 对 GPU 和 CPU 使用不同的管道?
- javascript - 我设法在谷歌表上附加和上传,不工作....我知道它的脚本或 onclick= 但卡住了任何帮助都会得到赞赏
- android - 如何更改 Android Studio 布局编辑器背景颜色