【问题标题】:Target window.opener iframe目标 window.opener iframe
【发布时间】:2013-09-06 19:00:42
【问题描述】:

我可以像这样在 iframe 中写一些东西

window.frames[0].document.write(something);

但我正在尝试从同一域中打开的窗口向 iframe 写入内容。我不知道如何定位那个 iframe。我试过这样的事情:

window.opener.document.getElementById("iframe").write(something);

window.opener.frames[0].document.write(something);

但这没有用。关于我想要做什么的任何想法或问题?

*iframe 在父窗口上。我尝试从打开的窗口中定位它

【问题讨论】:

  • 所以要清楚。您正在使用iframe 打开一个新窗口。从父窗口(打开新窗口)中,您想在新打开的窗口的iframe中写一些东西?
  • 不,我正在打开一个新窗口。从那个窗口我尝试在父窗口的 iframe 中写一些东西

标签: javascript html iframe


【解决方案1】:

您需要使用 iframe 的 contentWindow 属性。试试这个。

父页面

<iframe id="ifr"></iframe>

<button onclick="window.open('child.html')">Open Window</button>

Child.html 页面

<button onclick="writeToParentIframe()">Write to Parent Iframe</button>

<script>
    function writeToParentIframe() {
        opener.document.getElementById("ifr").contentWindow.document.write("<h1>Hello World</h1>")
    }
</script>

【讨论】:

    猜你喜欢
    • 2015-10-07
    • 2011-01-30
    • 2023-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多