我正在使用 Flask 创建一个 Web 应用程序,我的网页中有一个 Iframe,它的内容是本地 html 文件。我想访问此 html 文件属性并更新它们(更改 iframe 内的 onclick 文本 - 单击一个 iframe 时,我想更改页面中所有 iframe 中的内容,这就是我处理点击的原因主窗口。
<iframe height="300px" width="100%" name="iframe" src="static/iframe1.html"></iframe>
所以我想访问 iframe 文档,我尝试使用
document.getElementById("iframe").contentDocument.getElementById("iframe_div").innerHTML = data.result[0];
但是我收到错误
Uncaught TypeError: Cannot read property 'contentDocument' of null
你能帮我理解我正在做的事情出了什么问题吗?
最佳答案
显示错误是因为您试图获取 ID 为“iframe”的元素。
应该是:
<iframe id="iframe" height="300px" width="100%" name="iframe" src="static/iframe1.html"></iframe>
除此之外,您无法更改 iframe 行为
关于javascript - 从主窗口访问 iframe 内部属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39364409/